尝试将date_select与年,月,日发布到数据库但无法发布。我收到以下未经许可的参数。
这是binding.pry结果
17: def create
=> 18: binding.pry
19: @timeslot = Timeslot.new(timeslot_params)
20:
21: if @timeslot.save
22: flash[:notice] = "Your timeslot was created."
23: redirect_to timeslots_path(@timeslot)
24: else
25: render :new
26: end
27: end
[1] pry(#<TimeslotsController>)> params
=> {"utf8"=>"✓",
"authenticity_token"=>"9QH4nSgC9ov2Rj+cm/hruebc94OI63eUWJZoqWL9qzM=",
"timeslot"=>{"name"=>"5.30pm", "starts_at(1i)"=>"2014", "starts_at(2i)"=>"11", "starts_at(3i)"=>"13"},
"commit"=>"Create Timeslot",
"action"=>"create",
"controller"=>"timeslots"}
[2] pry(#<TimeslotsController>)> params.require(:timeslot).permit(:name, :"starts_at(1i).to_i", :"starts_at(2i).to_i", :"starts_at(3i).to_i")
Unpermitted parameters: starts_at(1i), starts_at(2i), starts_at(3i)
=> {"name"=>"5.30pm"}
[3] pry(#<TimeslotsController>)> params.require(:timeslot).permit(:name, starts_at: [])
Unpermitted parameters: starts_at(1i), starts_at(2i), starts_at(3i)
=> {"name"=>"5.30pm"}
[4] pry(#<TimeslotsController>)> params.require(:timeslot).permit(:name, starts_at: [(1i), (2i), (3i)])
Unpermitted parameters: starts_at(1i), starts_at(2i), starts_at(3i)
=> {"name"=>"5.30pm"}
[5] pry(#<TimeslotsController>)>
这是控制器强大的参数白名单
def timeslot_params
params.require(:timeslot).permit(:name, :"starts_at(1i).to_i", :"starts_at(2i).to_i", :"starts_at(3i).to_i")
end
这是表格
<div class='well'>
<%= simple_form_for @timeslot do |f| %>
<div class='control-group'>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class='control-group'>
<%= f.label :starts_at %>
<%= f.date_select :starts_at %>
</div>
<%= f.submit(@timeslot.new_record? ? 'Create Timeslot' : 'Update Timeslot', class: 'btn btn-primary')%>
<% end %>
</div>
我正在使用Rails 4.1.6 / ruby 2.1.3p242(2014-09-19修订版47630)[x86_64-darwin13.0]。
“starts_at”的数据库架构类型采用日期时间格式。
提前感谢任何帮助。
答案 0 :(得分:3)
如果 starts_at作为模型属性,请添加以下内容:
params.require(:timeslot).permit(:name, :starts_at)
如果没有在模型中添加attr_accessible :starts_at
,那么
params.require(:timeslot).permit(:name, :starts_at)
它会起作用。