f.collection_select: what are the parameters?

The rails docs are so horrible on the collection_select method. They need to show a *complete* form, and show two examples: one that calls f.collection_select(), and another that calls collection_select(), and explain the differences.

Given this line:

f.collection_select :topic, Topic.all, :id, :category

What the !@#$!@#$ is :topic?

the field name:

[collection_select](http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select)(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)

will generate:

<[select](http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select) name="post[author_id]">
  <option value="">Please [select](http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select)</  option>
<option value="1" selected="selected">D. Heinemeier Hansson</option>
  <option value="2">D. Thomas</  option>
<option value="3">M. Clark</option>
</select>

look fella, for docs look at http://apidock.com/rails is awesome and much better

see ya and any questions, please just ask

:slight_smile:

Given this line:

f.collection_select :topic, Topic.all, :id, :category

If I give rails a collection (Topic.all), and the value attribute of the <option> tag (:id), and the display text (:category), why would rails need to know anything else?

Leoncio Caminha wrote in post #1016087:

the field name: collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true) will generate:

<select name="post[author_id]">   <option value="">Please select</option>   <option value="1" selected="selected">D. Heinemeier Hansson</option>   <option value="2">D. Thomas</option>   <option value="3">M. Clark</option> </select>

Okay, so it looks like the first two arguments determine the name of the select? But don't the arguments change if you call:

f.collection_select(....)

???

7stud -- wrote in post #1016089:

Leoncio Caminha wrote in post #1016087:

the field name: collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true) will generate:

<select name="post[author_id]">   <option value="">Please select</option>   <option value="1" selected="selected">D. Heinemeier Hansson</option>   <option value="2">D. Thomas</option>   <option value="3">M. Clark</option> </select>

Okay, so it looks like the first two arguments determine the name of the select?

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.

7stud -- wrote in post #1016089:

Leoncio Caminha wrote in post #1016087:

the field name: collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true) will generate:

<select name="post[author_id]"> <option value="">Please select</option> <option value="1" selected="selected">D. Heinemeier Hansson</> <option value="2">D. Thomas</option> <option value="3">M. Clark</option> </select>

Okay, so it looks like the first two arguments determine the name of the select?

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:

<select name="post[topic_id]" id="post_topic_id" size="1"> <option value="">Please choose...</option> <option value="42">Life, the Universe, and Everything</option> ...

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:

<select name="post[favorite_color]" id="post_favorite_color" size="1"> <option value="">Please choose...</option> <option value="red">Red</option> ...

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

Walter Davis wrote in post #1016097:

Hi,

Thanks for the response.

>

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?

Why should I have to assume that? Why can't the form select with all the users and their ids, and then the action that the form is submitted to sends a birthday email to the selected user? Or is that what form_for(@user) locks me into?

dude, its a encapsulete form in html....one specie of object orientation you know? its simple...open your mind its easy

any doubts, please, ask

:slight_smile:

Enviado via iPad

Leoncio Caminha wrote in post #1016134:

dude, its a encapsulete form in html....one specie of object orientation you know? its simple...open your mind its easy

Don't bother making posts like that.

Walter Davis wrote in post #1016097:

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

Why should I have to assume that? Why can't the form select with all the users and their ids, and then the action that the form is submitted to sends a birthday email to the selected user? Or is that what form_for(@user) locks me into?

It's what's in the action processing the form data that dictates what the data submitted by the form should look like. form_for(@user) and using example like Walter's go hand in hand with being able to just do @user.update_attributes(params[:user])

Fred

The rails docs are so horrible on the collection_select method. They need to show a *complete* form, and show two examples: one that calls f.collection_select(), and another that calls collection_select(), and explain the differences.

The different between random_helper and f.random_helper is that in the first case, the first argument is used to determine what model object is being edited. In the second case that argument is superfluous since the form builder (f) already has that information.

Given this line:

f.collection_select :topic, Topic.all, :id, :category

What the !@#$!@#$ is :topic?

the name of the attribute being edited

Fred

Walter Davis wrote in post #1016097:

>

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:

<select name="post[topic_id]" id="post_topic_id" size="1"> <option value="">Please choose...</option> <option value="42">Life, the Universe, and Everything</option> ...

I don't know anything about associations yet. Is topic_id a field in the Post model when a Post has_one :topic?

. . . . . . .

See the Rails Guide on Associations. Make sure you completely understand what is there, it is vital to understanding Rails.

Colin

Yes.

Walter

Colin Law wrote in post #1016241:

<%= form_for @post do |f| %>

I don't know anything about associations yet. Is topic_id a field in the Post model when a Post has_one :topic?

See the Rails Guide on Associations. Make sure you completely understand what is there, it is vital to understanding Rails.

I read it. Not very good. I'll have to read up on some database theory.

Walter Davis wrote in post #1016265:

It would be useful to know in particular what you found lacking in the Associations Guide, so that we (the community) can improve it. My own first impression of it is that it pretty much assumes the reader is already familiar with foreign keys (fields like 'topic_id'). Is that the same thing you found?

Chris

No, Post has a topic_id field if Post belongs_to :topic.

If Post has_one topic then Topic belongs_to Post so Topic will have a post_id

Colin

What do you mean "not very good", do you mean your understanding of it is not very good?

Colin