interboy
interboy

Reputation: 886

Rails collection_select custom name attribute

I have the following collection select which acts as a filter in a Rails app.

<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
   <%= collection_select :doctor, :id, @doctors, :id, :full_name, {:include_blank => 'All'} %>
<% end %>

This always generates a name attribute of the select element like name="doctor[id]" which results in the browser to ?utf8=✓&doctor%5Bid%5D=1, which is not quite readable.

How can I change the name attribute to just name = "doctor" or basically just remove the brackets from it?

Upvotes: 5

Views: 4922

Answers (2)

Ronnie Cheng
Ronnie Cheng

Reputation: 71

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

The collection_select method contains the parameters "options" and "html_options". "options" allow you to add specific information, like {:include_blank => 'All'}, but does not replace html attributes.

You have to add the name to the next hash, like this:

<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
   <%= collection_select :doctor, :id, @doctors, :id, :full_name, {:include_blank => 'All'}, {:name => 'doctor'} %>
<% end %>

Upvotes: 7

n_i_c_k
n_i_c_k

Reputation: 1534

Have you tried:

<%= form_tag( "/appointments", :method => "get", :id => "filter_form") do %>
    <%= collection_select :doctor, :id, @doctors, :id, :full_name, {:include_blank => 'All', :name => 'doctor'} %>
<% end %>

Upvotes: 0

Related Questions