On Aug 10, 2011, at 9:53 PM, 7stud – wrote:
option>
And even that isn’t entirely correct. Apparently, the name of the
select has to be of the form:
an_existing_model[:an_existing_field_name_in_that_model]
which doesn’t make any sense to me.
I’m not sure why that is – you’re inside of a form builder, building
a form for an object that you plan to modify when you submit that
form, right? What you’re describing here is precisely how Rails builds
forms, and what it expects to receive when you send your controller
the params hash.
Now let’s say you want to add a select field to your Post to choose a
Topic. Post has_one :topic.
<%= form_for @post do |f| %>
<%= f.collection_select :topic_id, Topic.all, :id, :name, :prompt =>
true %>
That gives you:
Please choose...
Life, the Universe, and Everything
...
If you wanted to pick a favorite color from an array, you could do
that like this:
<%= f.collection_select :favorite_color %W(red green
blue), :to_s, :titleize, :prompt => true %>
That gives you:
Please choose...
Red
...
In either case, when you create or update a Post, you send the hash
params[:post] to its ccontroller, and all the parameters nested inside
of that hash are untwisted in there and assigned to the object.
It’s really very flexible.
Walter