[英]Passing Parameters Using Rails 4 date_field
Below in is the HTML5 output I am looking for. 下面是我正在寻找的HTML5输出。 The Goal is to pass a parameter from the dropdown calendar to my controller and refresh the page with the input selected. 目标是将参数从下拉日历传递到我的控制器,并使用选定的输入刷新页面。
<form action="/cats/<%=@cats.number_id %>">
<input type="date" name="as_of_date" id="as_of_date" value="<%= Time.parse(@as_of).strftime("%Y-%m-%d") %>">
<input type="submit" value="Re-calculate">
</form>
I noticed that format of the date_field helper is this according to http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormHelper/date_field : 我注意到根据http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormHelper/date_field,date_field帮助器的格式是这样的:
@user.born_on = Date.new(1984, 1, 27)
date_field("user", "born_on", value: "1984-05-12")
# => <input id="user_born_on" name="user[born_on]" type="date" value="1984-05-12" />
Beyond this point I do not know how to effectively use the date_field helper. 除了这一点,我不知道如何有效地使用date_field帮助器。
In Rails, you would typically use a form_for
helper to generate a form. 在Rails中,通常会使用form_for
助手来生成表单。 Inside that form you can then use a date_field
helper, like so: 然后,您可以在该表单内使用date_field
帮助器,如下所示:
<%= form_for @cat do |f| %>
<%= f.date_field :as_of_date %>
<%= f.submit 'Re-calculate' %>
<% end %>
In the controller, you would initialize @cat
as an instance of an ActiveRecord
model named Cat
. 在控制器中,您可以将@cat
初始化为名为Cat
的ActiveRecord
模型的实例。 This will not generate exactly what you asked for, but hopefully close enough: 这不会完全生成您想要的内容,但希望足够接近:
<form class="edit_cat" id="edit_cat_89" action="/cats/89" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="kdCpL...wEDA==">
<input value="2015-10-13" type="date" name="cat[as_of_date]" id="cat_as_of_date">
<input type="submit" name="commit" value="Re-calculate">
</form>
If the date is not a cat's attribute, you might want to use something like this: 如果日期不是猫的属性,则可能需要使用以下内容:
<%= form_for "/cats/#{cat_id}" do |f| %>
<%= date_field_tag :as_of_date, Date.new(2015, 10, 13) %>
<%= f.submit 'Re-calculate' %>
<% end %>
HTH! HTH!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.