Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Wrap parameter coming from form in a nested hash

I have the following form for search 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 parameters for filtering in a nested hash so that I have something as follows in params: {"utf8"=>"✓", "search"=>"term", "action"=>"index", "controller"=>"search", "filter" => {"field" => "value"}}. Note that filter params are nested

I couldn't find a way to do this using standard rails form helpers. Is there any way to do that?

like image 230
roman Avatar asked May 20 '13 19:05

roman


1 Answers

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

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

If you wanted something in the form:

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

You would add another []:

<%= check_box_tag 'filter[subfilter][field]', 'value', true %>
like image 96
PinnyM Avatar answered Sep 23 '22 20:09

PinnyM