【发布时间】:2014-04-24 08:19:55
【问题描述】:
我遇到了批量分配错误。
Can't mass-assign protected attributes: 1, 2, 3, 4, 5, 6, 7
这些数字代表此循环中的迭代:
<% (1..7).each do |i| %>
<%= select_tag "hour[#{i}][day]", options_for_select(days_hours) %>
<% end %>
这是在我的模型中:
attr_accessible :day, :open_time, :close_time
我正在尝试创建一个这样的数组:
"hour"=>{
"1"=>{"day"=>"Sunday","open_time"=>"6","close_time"=>"6"},
"2"=>{"day"=>"Sunday","open_time"=>"6","close_time"=>"6"},
"3"=>{"day"=>"Sunday","open_time"=>"6","close_time"=>"6"}
}
我正在尝试将新行中的每个迭代保存到数据库中
def create
@hour = @hourable.hours.new(params[:hour])
end
如何修复迭代质量分配?还是我做错了?
谢谢!
【问题讨论】:
-
请说明您希望应用程序做什么。很难从您的代码中分辨出来。什么是班级? select_tag 的用户设置是什么?
-
“我正在尝试创建一个这样的数组:”这是一个哈希,这可能是您的问题的一部分,该数组看起来像这样:
hours = [{"day"=>"Sunday","open_time"=>"6", "close_time"=>"6"}, {"day"=>"Monday","open_time"=>"6", "close_time"=>"6"},{"day"=>"Tuesday","open_time"=>"6", "close_time"=>"6"}] -
@Isotope 是有道理的。你认为你可以帮我在不需要数字的地方得到它吗?我尝试删除
#{i},并且只有hour[day]之类的东西,我收到Internal Server Error并带有以下消息:expected Array (got String) for paramday'` -
@andrewliu:请更新您的问题,详细说明要求。可能是您可以提供的整个视图内容,以及您希望如何显示选择标签、其值以及您希望如何将其保存在数据库中,
:open_time、:close_time等值是什么。 -
<%= select_tag "hour[i][open_time]", options_for_select(days_hours) %> <%= select_tag "hour[i][close_time]", options_for_select(days_hours) %> <%= hidden_field_tag "hour[i][day]", value: i %>那么你只需要把day int变成一个字符串
标签: ruby-on-rails ruby ruby-on-rails-3 mass-assignment