访问date()变量从rails的date helper发送

编程入门 行业动态 更新时间:2024-10-27 22:28:55
本文介绍了访问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])

和 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发送

本文发布于:2023-11-23 17:32:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   date   helper   rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!