Rails collection_select custom name attribute

html, ruby-on-rails

Solution

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 %>

Problem

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?

Original source