Each post has a single category, which I’m filling a drop down list by
using this:
#view
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!
ryan heath wrote:
Each post has a single category, which I’m filling a drop down list by
using this:
#view
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
On Sunday, July 16, 2006, at 5:52 AM, ryan heath wrote:
Post, I want it to get whichever category is selected, and save it in
Rails mailing list
[email protected]
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