他的助手代码
<%= date_select(game,release_date order => [:day,:month,:year])%>如何在控制器中访问返回的变量?
谢谢
解决方案date_select helpper什么Rails调用多参数属性。也就是说, release_date 属性的值分割为由 date_select helper。 date_select 按照惯例命名3个下拉菜单,以便在控制器中执行以下操作:
@game = Game.new(params [:game])和 release_date 属性将填入选定的日期。
或
@ game.update_attributes(params [:game])更新现有记录。
如果您检查参数或查看日志文件,您将看到3个单独的组件:
params [game] [release_date(1i)]#年 params [game] [release_date(2i )]#月份 params [game] [release_date(3i)]#一天虽然您可以直接访问这些个别元素,但似乎并不是一个好主意。 date_select helper真正被设计为与ActiveRecord的 attributes = setter组合使用,在创建一个新对象或更新属性。
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
解决方案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 dayWhilst 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.
更多推荐
访问date()变量从rails的date helper发送
发布评论