0

Is it possible to fire up an custom action when user clicks 'Search' button on search form?

There is an mechanism in our app to save every URL the app has hit. In our search form, when clicking 'Search' button, there will bring up the search result page. The problem is that the URL for the search result form was not saved. The Back button brings back the search page (for setup search params) instead of the search result page (because its URL was not saved).

Here is the search form for model configs:

  <h4>Search Form></h4>
  <%= simple_form_for @config, :method => :get, :url => search_result_configs_path  do |f| %>
      <%=render  :partial => 'search_params', :locals => {f: f} %>

      <%= f.button :submit, t('Search') %>
  <% end %>

The URL for the search result looks like this (with the search params set by user) after user clicks Search button:

http://localhost:3000/configs/search_results?utf8=%E2%9C%93&engine_config[start_date_s]=&engine_config[end_date_s]=&engine_config[engine_id_s]=1&engine_config[argument_name_s]=&engine_config[commissioned_s]=&commit=%E6%90%9C%E7%B4%A2

This is the URL we would like the app to remember. We figure we need custom action triggered when a user clicks 'Search' button. Is it possible?

user938363
  • 9,990
  • 38
  • 137
  • 303
  • 1
    We probably can insert custom action in action serach_results in controller after user clicks Search buttun. – user938363 Jul 06 '14 at 22:23

1 Answers1

1

Route

Firstly, calling a custom application is actually quite a simple process - you just need to call its route:

#config/routes.rb
resources :search do
    collection do
        get :custom_action
    end
end

This will allow you to use the likes of form_tag to call the custom route:

#app/views/your_controller/view.html.erb
<%= form_tag search_custom_action_path, method: :get do %>
    ...
<% end %>

--

Form

Secondly, you're using simple_form for your search form.

This is completely fine, but the problem you have here is that when you use this, it has to have a ActiveRecord object to populate the form with. This is probably where you're getting confused, as to do this, you need ot make sure @config is available every time you load that form, which I imagine can be a lot.

We've created a search form here:

enter image description here

Although in Rails 4, we used a form_tag for this form, as it allowed us to create & display the form where-ever we need in the app. This allows us to pass the required params through the form & access them on the other side

--

Params

You mention you want to "save the URL" - what do you mean by this?

Surely you'd prefer to save the params?

If this is true, the way to do this is actually relatively simple - you'll get access to the params hash in your controller when you send the request through:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
  def custom_action
     params[:your_param] #-> this is accessible here
  end
end

The bottom line is if you wanted to save the query strings, you'll have to create a model called Search or similar, allowing you to pass the params through when you process the custom action in your controller, just like you would any other ActiveRecord object

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147