csn
February 10, 2006, 12:08am
1
I’ve looked through the docs and the wiki and can’t
figure out how to go about generating select options
and the update function for an item that has a HABTM
relationship.
I have a ‘parks’ table and the park model has a HABTM
to the ‘states’ table. On the park edit page, I’d like
a multiple select box to appear with all states show,
and those in the parks_states table selected. Does
anybody have some example code?
Thanks!
csn
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...
csn
February 10, 2006, 2:12am
2
CSN <cool_screen_name90001@…> writes:
I’d don’t use the multiple-choice select very often, but something like:
options_from_collection_for_select(State.find(:all), :id, :name,
@park.states.collect {|s| s.id})
should work, right? Assuming State is your model, and the value of the
option is
State#id, and the text is State#name, and @park is your current Park.
csn
February 10, 2006, 2:24am
3
— KW [email protected] wrote:
I have a ‘parks’ table and the park model has a
but something like:
options_from_collection_for_select(State.find(:all),
:id, :name,
@park.states.collect {|s| s.id})
should work, right? Assuming State is your model,
and the value of the option is
State#id, and the text is State#name, and @park is
your current Park.
Yeah - here’s what I came up with:
edit template
<%= options_from_collection_for_select
State.find(:all, :order=>'name'), 'id', 'name',
@park.states.collect {|state| state.id } %>
update method
@park = Park.find(params[:id])
@park.state_ids = params[:park_states]
@park.update_attributes (params[:park])
if @park.errors.empty ?
redirect_to :action=>:list
else
render :action=>:edit
end
If anybody can improve and/or simplify it, please feel
free! Perhaps I’ll stick it in the wiki.
csn
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
[object