[Rails] Re: HELP - Save drop down list using "collection_select"

Scott Helfrich
Sun, 16 Jul 2006 07:27:17 -0000

ryan heath wrote:
> Each post has a single category, which I''m filling a drop down
list by
> using this:
>
> #view
> <label
for="post_type_id">Category
> <%= collection_select(:id, :type_id, Type.find(:all), :id, :category)
%>
>
>
> In my Post model I have a field called "type_id" which is to be
filled
> by the id in the Type table upon selection.  When I click
"create" on my
> Post, I want it to get whichever category is selected, and save it in
> the "type_id" field.  I don''t know if I''m going
about this wrong or not.
> I''ve never done this before, and can''t get it to work.  I
get the drop
> down list to populate from the Type table, but that''s it.
>
> Also, what about editing?  When I edit, will it load the selected type?
>
> Thanks a lot for any thoughts on this!

Read through this thread and I think it will help you. I struggled with
this several days ago and some good folk on the board helped me out.
I''m
new to this myself and I''m not confident I can explain it well enough
in
the right terminology yet to be accurate as I should be, and I don''t
want to confuse you, but what you want to do, I wanted to do - and have
done now and it works very well.

http://www.ruby-forum.com/topic/72227#new

Try that thread. Of course you will have to map my fields to your own.
Read the whole thread as I was not interested in returning an id; I
wanted just the cateory field value itself.

And yes, it will load the one from the record when editing! That was
something I had to have happen.

I hope this helps.

Scott

--
Posted via http://www.ruby-forum.com/.