Ruby on rails 4 rails 4更新嵌套属性时不执行&x27;行不通
我尝试了所有关于这个问题的答案,但我仍然无法解决我的问题。 我必须检查控制器(和模型):检查表和字段Ruby on rails 4 rails 4更新嵌套属性时不执行&x27;行不通,ruby-on-rails-4,has-many,Ruby On Rails 4,Has Many,我尝试了所有关于这个问题的答案,但我仍然无法解决我的问题。 我必须检查控制器(和模型):检查表和字段 class NcchecklistsController < ApplicationController def update if @ncchecklist.update(ncchecklist_params) redirect_to ncchecklists_url, notice: 'Check-list sauvgardée.' else r
class NcchecklistsController < ApplicationController
def update
if @ncchecklist.update(ncchecklist_params)
redirect_to ncchecklists_url, notice: 'Check-list sauvgardée.'
else
render :index
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ncchecklist
@ncchecklist = Ncchecklist.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def ncchecklist_params
params.require(:ncchecklist).permit(:name, ncfield_params: [:value, :id])
end
感谢您的帮助控制器的强参数只是有点不合适。 它们应该是${model}\u属性,而不是${model}\u参数
def ncchecklist_params
params.require(:ncchecklist).permit(:name, ncfields_attributes: [:value, :id])
end
您可以在问题结束时看到它被发送到您的原始日志中,但强参数正在将其过滤掉。我很遗憾,它是ncfields\u属性,而不是ncfield\u属性,现在我想为每个项目分配一个检查列表(我有项目类和控制器)。有一个简单的方法吗?
class Ncchecklist < ActiveRecord::Base
has_many :ncfields, dependent: :destroy
attr_accessible :ncfields
accepts_nested_attributes_for :ncfields
end
class Ncfield < ActiveRecord::Base
belongs_to :ncchecklist
end
<h1> Vos check-list </h1>
<% @ncchecklists.each do |ncchecklist| %>
<%= form_for(ncchecklist) do |ncchecklist_form| %>
<h3><%= ncchecklist.name %></h3>
<% ncchecklist.ncfields.each do |field| %>
<%= ncchecklist_form.fields_for :ncfields, field do |ncfield_form| %>
<p>
<%= ncfield_form.label :name, 'Tâche : ' %>
<%= field.name %> :
<%= ncfield_form.label 'value_false', 'Non' %>
<%= ncfield_form.radio_button :value, false %>
<%= ncfield_form.label 'value_true', 'Oui' %>
<%= ncfield_form.radio_button :value, true %>
</p>
<% end %>
<% end %>
<%= ncchecklist_form.submit 'Sauvegarder '+ncchecklist.name %>
<% end %>
<% end %>
Started PATCH "/redmine/ncchecklists/2" for ::1 at 2016-12-21 15:58:48 +0100
Processing by NcchecklistsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "ncchecklist"=>{"ncfields_attributes"=>{"0"=>{"value"=>"false", "id"=>"2"}, "1"=>{"value"=>"false", "id"=>"3"}}}, "commit"=>"Sauvegarder Chef de projet", "id"=>"2"}
Current user: anonymous
Redirected to http://localhost/redmine/ncchecklists
def ncchecklist_params
params.require(:ncchecklist).permit(:name, ncfields_attributes: [:value, :id])
end