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 then show two examples: one that
calls
f.collection_select(), and another that calls collection_select(), and
then
explain the differences.

Given this line:

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

What the [email protected]#[email protected]#$ is :topic? Alternatively, why would :topic even be
needed as a parameter? What information does that provide that
Topic.all doesn’t?

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

Please select D. Heinemeier H. D. Thomas M. Clark

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:

Em 10/08/2011, s 21:52, 7stud – escreveu:

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

tag (:id), and the display text (:category), why would rails need to know anything else?

Leoncio C. wrote in post #1016087:

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

Please select D. Heinemeier H. D. Thomas M. Clark

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(…)

???

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

7stud – wrote in post #1016089:

Leoncio C. wrote in post #1016087:

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

Please select D. Heinemeier H. D. Thomas M. Clark

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. If the argument for the value
attribute of an is :id, then wouldn’t the natural name of the
select be:

the_model[:id]

???

But don’t the arguments change if you call:

f.collection_select(…)

???

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

Em 10/08/2011, s 22:53, 7stud – [email protected] escreveu:

Walter D. wrote in post #1016097:

On Aug 10, 2011, at 9:53 PM, 7stud – wrote:

Hi,

Thanks for the response.

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?

Why should I have to assume that? Why can’t the select present 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 does
form_for(@user) force me into creating or updating a user?

Leoncio C. 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.

On Aug 11, 1:52am, 7stud – [email protected] wrote:

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 [email protected]#[email protected]#$ is :topic?

the name of the attribute being edited

Fred

Walter D. wrote in post #1016097:

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

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

.
.
.
.
.
.
.

On Aug 11, 5:46am, 7stud – [email protected] wrote:

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

On 11 August 2011 19:02, 7stud – [email protected] wrote:

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

Colin

On Aug 11, 2011, at 2:02 PM, 7stud – wrote:

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

Yes.

Walter

On 12 Aug 2011, at 01:15, 7stud – wrote:

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

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

Colin L. wrote in post #1016241:

On 11 August 2011 19:02, 7stud – [email protected] wrote:

<%= 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 D. wrote in post #1016265:

On Aug 11, 2011, at 2:02 PM, 7stud – wrote:

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

Yes.

Thanks.

On 11 August 2011 23:38, Walter Lee D. [email protected] wrote:

On Aug 11, 2011, at 2:02 PM, 7stud – wrote:

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

Yes.

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

On 12 August 2011 01:15, 7stud – [email protected] wrote:

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.

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

Colin