Reputation: 2409
Rails 2.3.11
I did read this answer, but it's not working for me.
I would like the default option for this selection box to be the event_id
passed through the URL.
f.select :event_id, @events, :selected => url_args["event_id"]
An example @events
is[["SC 2 Tournament", 195], ["Obstacle Course", 196], ["Mortal Combat", 197]]
The following also didn't work:
Thank you!
Upvotes: 0
Views: 359
Reputation: 1359
This is a lot easier if you use the collection_select helper:
f.collection_select :event_id, @events, :id, :name
Then to select the default option (and have that be selected on pageload), you can simply assign it to whatever Object it is that the form is for within the controller. eg like this:
def new
@events = Event.all
@thing = Thing.new(:event => @events.first)
end
I'm not sure where your url_args
comes from, but I'm guessing it's probably from a param in the URL, in which case you can do this:
Thing.new(:event_id => params[:event_id])
One last thing - collection_select
won't quite work with @events
as you've specified it, as you're using a nested Array, when it's expecting an Array of Objects that it can call id
and name
on in order to retrieve the values and display text for the select options. To fix that, simply redefine @events
within your controller, using one of the ActiveRecord finders, such as Event.all
or Event.find(..)
.
Make sense?
Upvotes: 2