访问date()变量从rails的date helper发送 [英] Accessing date() variables send from the date helper in rails

查看:116
本文介绍了访问date()变量从rails的date helper发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hay,不知道我是否错过了rails的助手,但是我使用了date()帮助器,现在我不知道如何访问返回的变量。



他的助手代码

 <%= 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])

和<$新游戏中的c $ c> release_date 属性将填入选定的日期。



  @ game.update_attributes(params [:game])

更新现有记录。



如果您检查参数或查看日志文件,您将看到3个单独的组件:

  params [game] [release_date(1i)]#年
params [game] [release_date(2i )]#月份
params [game] [r​​elease_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 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.

这篇关于访问date()变量从rails的date helper发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆