views:

360

answers:

1

Hay, not sure if i've missed the point of the helpers in rails, but i used the date() helper and now i don't know how to access the variables returned.

Heres the helper code

<%=date_select("game", "release_date", :order => [:day, :month, :year])%>

How do i access the returned variables in a controller?

Thanks

+3  A: 

The date_select helper makes use of what Rails calls multi parameter attributes. That is, the value of the release_date attribute is split across the 3 dropdowns for day, month, year which are created by the date_select helper. date_select names the 3 dropdowns following a convention such that in your controller you can do:

@game = Game.new(params[:game])

and the release_date attribute on the new game will be populated with the date selected.

or

@game.update_attributes(params[:game])

to update an existing record.

If you inspect params or look in the log file you will see the 3 individual components:

params["game"]["release_date(1i)"] # the year
params["game"]["release_date(2i)"] # the month
params["game"]["release_date(3i)"] # the day

Whilst you could access these individual elements directly it doesn't seem like a good idea. The date_select helper is really designed to be used in combination with the attributes= setter of ActiveRecord which is used when creating a new object or updating attributes.

mikej
the Game model has a date() field called "release_date". Would it be correct to do Game.create(:name => name, :release_date => params[:release_date]) ?
dotty
If the form has been creating using the standard helpers then the name, release_date etc will be nested inside as params[:game][:name] etc. There isn't a single entry for params[:game][:release_date]. You could however do Game.create(params[:game]). create is just the same as using .new followed by a save.
mikej