Rails: Wrap parameter coming from a form in a nested hash

I have the following search form, which should also perform filtering:

<%= form_tag search_index_path, method: :get, id: 'search_form' do %> <%= text_field_tag :search, params[:search] %> <%= check_box_tag 'filter param', 'yes', true %> <%= submit_tag "Search", name: nil, class: 'btn' %> <% end %> 

What I'm trying to do now is to wrap the filtering parameters in a nested hash so that I have something like the following in the parameters: {"utf8"=>"βœ“", "search"=>"term", "action"=>"index", "controller"=>"search", "filter" => {"field" => "value"}} . Please note that the filter options are nested.

I could not find a way to do this using standard rails creating helpers. Is there any way to do this?

+4
source share
1 answer

Use [] around the nested keys you want. In your example, change your check_box to:

 <%= check_box_tag 'filter[field]', 'value', true %> 

If you want something in the form:

 { "filter" => { "subfilter" => { "field" => "value" } } } 

You would add another [] :

 <%= check_box_tag 'filter[subfilter][field]', 'value', true %> 
+8
source

Source: https://habr.com/ru/post/1481820/


All Articles