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

Kevin Olbrich
Sun, 16 Jul 2006 12:33:45 -0000

On Sunday, July 16, 2006, at 5:52 AM, 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!
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails

This usually works

<%= select ''post'', ''type_id'',
Type.find(:all).map {|x| [x.name, x.id]} %>

I would avoid using ''Type'', ''type'', or any
variation of this if you can.
 ''type'' is a magic column name used for single table
inheritance and
will probably cause all sorts of weird bugs.  It''s also a ruby keyword.


_Kevin
www.sciwerks.com

--
Posted with http://DevLists.com.  Sign up and save your mailbox.