我正在rails 4中构建嵌套表单。我不断收到此错误
我的_form.html.erb为
<%= nested_form_for (@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
_question.html.erb
<p>
<%= f.label :content, "Question" %><br />
<%= f.text_area :content, :rows => 3 %><br />
<%= f.label :subject, "Question" %><br />
<%= f.text_field :subject %><br />
<%= f.link_to_remove "Remove this task" %>
<p><%= f.link_to_add "Add a questions",:questions %></p>
</p>
project.rb
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
修改
question.rb
class Question < ActiveRecord::Base
belongs_to :project
end
项目控制器
def new
@project = Project.new
3.times do
question = @project.questions.build
end
def project_params
params.require(:project).permit(:name)
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
端
我用“ nested_form ”gem给出错误Invalid association. Make sure that accepts_nested_attributes_for is used for :questions association.
请帮我摆脱这个错误
问题控制器def: -
def question_params
params.require(:question).permit(:project_id, :subject, :content)
end
答案 0 :(得分:1)
你的问题可能是你的强参数
尝试将projects_controller中的project_params更改为
def project_params
params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])
end
而且,您的控制器代码应如下所示
def new
@project = Project.new
3.times do
@question = @project.questions.build
end
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
private
def project_params
params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])
end
此外,您必须寻找 Strong Parameters with accepts_nested_attributes_for 。
<强>更新强>
尝试将_form.html.erb
更改为
<%= nested_form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :ff => builder %> #changed to ff to avoid confusion
<% end %>
<p><%= f.link_to_add "Add a questions",:questions %></p> #this line it should be here.
<div class="actions">
<%= f.submit %>
</div>
<% end %>
您的_question_fields.html.erb
为
<p>
<%= ff.label :content, "Question" %><br />
<%= ff.text_area :content, :rows => 3 %><br />
<%= ff.label :subject, "Question" %><br />
<%= ff.text_field :subject %><br />
<%= ff.link_to_remove "Remove this task" %>
</p>