有没有办法对加载到 Rails 表单输入中的数据调用方法? (具体来说是一个文本字段)
我正在使用 Rails 3.0.5,并且出于 这个问题,我需要在 text_field 输入上调用 utc_to_local 方法来获取日期时间。
我尝试过直接在视图中以各种方式调用该方法(作为起点),但我似乎找不到神奇的组合(之前没有在表单字段上调用过方法)。
/events/edit.html.erb(完整文件):
<h1>Edit This Event</h1>
<%= form_for(@event) do |f| %>
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Update Event" %>
</div>
<% end %>
events/_fields.html.erb(仅相关行:)
<div class="field">
<%= f.label :time_start, "Start Time" %><br />
<%= f.text_field :time_start, :class => "datetimefield" %>
</div>
I'm using Rails 3.0.5, and for the reasons specified in this question, I need to call a utc_to_local method on the text_field input for a datetime.
I've tried calling the method in various ways directly in the view (as a starting point), but I can't seem to find the magic mix (haven't called a method on a form field before).
/events/edit.html.erb (full file):
<h1>Edit This Event</h1>
<%= form_for(@event) do |f| %>
<%= render 'fields', :f => f %>
<div class="actions">
<%= f.submit "Update Event" %>
</div>
<% end %>
events/_fields.html.erb (relevant lines only:)
<div class="field">
<%= f.label :time_start, "Start Time" %><br />
<%= f.text_field :time_start, :class => "datetimefield" %>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
直接回答:
但是......你为什么不使用
in_time_zone
?这将导致创建应用程序助手:
在您看来:
To answer straight:
But... why don't you use
in_time_zone
?Which would lead to the creation of an Application Helper:
And in your view: