当我使用隐藏字段
时,我获得了nil值<%= form_for @hour do |f| %>
<%= f.hidden_field "days[]", :value => "Sunday" %>
<%= f.hidden_field "days[]", :value => "Monday" %>
<%= f.hidden_field "days[]", :value => "Tuesday" %>
<%= f.hidden_field "days[]", :value => "Wednesday" %>
<%= f.hidden_field "days[]", :value => "Thursday" %>
<%= f.hidden_field "days[]", :value => "Friday" %>
<%= f.hidden_field "days[]", :value => "Saturday" %>
<% end %>
我的参数:
"days"=>[nil,nil,nil,nil,nil,nil,nil]
我做隐藏的字段错了吗?
由于
答案 0 :(得分:1)
检查以下代码是否有帮助。
<%= form_for @hour do |f| %>
<%= f.hidden_field "days[0]", :value => "Sunday" %>
<%= f.hidden_field "days[1]", :value => "Monday" %>
<%= f.hidden_field "days[2]", :value => "Tuesday" %>
<%= f.hidden_field "days[3]", :value => "Wednesday" %>
<%= f.hidden_field "days[4]", :value => "Thursday" %>
<%= f.hidden_field "days[5]", :value => "Friday" %>
<%= f.hidden_field "days[6]", :value => "Saturday" %>
<% end %>
PARAMS:
Parameters: {"utf8"=>"√",authenticity_token"=>"m84ly0Ok8NWKzqv2ZGZvtP1tRoSZ3M0OlL7W5W5Pk+I=",
"hour"=>{"days"=>{"0"=>"Sunday", "1"=>"Monday", "2"=>"Tuesday",
"3"=>"Wednesday", "4"=>"Thursday", "5"=>"Friday", "6"=>"Saturday"}},
"commit"=>"Create hour "}
更新
动态:
<%= form_for @hour do |f| %>
<%= f.hidden_field "days][", :value => "Sunday" %>
<%= f.hidden_field "days][", :value => "Monday" %>
<%= f.hidden_field "days][", :value => "Tuesday" %>
<%= f.hidden_field "days][", :value => "Wednesday" %>
<%= f.hidden_field "days][", :value => "Thursday" %>
<%= f.hidden_field "days][", :value => "Friday" %>
<%= f.hidden_field "days][", :value => "Saturday" %>
<% end %>
输出:
Parameters: {"utf8"=>"√", "authenticity_token"=>"jxCDmSoKjclXu4ySSWp/gW19Ejy4tY3NwdBW/l/+lRU=", "user"=>{"hour"=>["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]}, "commit"=>"Create Hour"}
答案 1 :(得分:1)
为什么你需要在视图中使用隐藏字段来获取参数中的日期名称数组?
您可以在控制器中执行此操作:
params.merge!('days' => Date::DAYNAMES)
将在params中给你:
"days"=>["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
或者,如果您需要hour
哈希中的天数:
params['hour'].merge!('days' => Date::DAYNAMES)
或者,如果您需要本地化名称,而不是Date::DAYNAMES
使用:
I18n.t(:'date.day_names')