Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无效关联。确保接受的\u嵌套的\u属性\u用于:问题关联_Ruby On Rails_Ruby On Rails 4_Nested Forms_Nested Form For - Fatal编程技术网

Ruby on rails 无效关联。确保接受的\u嵌套的\u属性\u用于:问题关联

Ruby on rails 无效关联。确保接受的\u嵌套的\u属性\u用于:问题关联,ruby-on-rails,ruby-on-rails-4,nested-forms,nested-form-for,Ruby On Rails,Ruby On Rails 4,Nested Forms,Nested Form For,我正在rails 4中构建嵌套表单。我不断遇到此错误 my_form.html.erb as <%= nested_form_for (@project) do |f| %> <% if @project.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@project.errors.count, "error") %> proh

我正在rails 4中构建嵌套表单。我不断遇到此错误

my_form.html.erb as

    <%= 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 %>
编辑

问题.rb

      class Question < ActiveRecord::Base

       belongs_to :project

       end
结束

我使用了“nested\u form”gem给出错误
无效关联。确保接受的\u嵌套的\u属性\u用于:问题关联。

请帮我摆脱这个错误

问题:-

      def question_params
      params.require(:question).permit(:project_id, :subject, :content)
      end

可能你的问题在于你的强参数

尝试将项目控制器中的项目参数更改为

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
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
end

private

def project_params
  params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])       
end
此外,您还必须查找

更新

尝试将您的
\u 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 %>
<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>

您的
问题
模型的
类名是什么?也发布你的控制器代码。你的问题模型中有内容属性吗?控制器代码-强参数规范?发布你的完整控制器代码不太清楚!您的控制器代码没有创建方法?@AnishShah很高兴提供帮助:)我无法编辑嵌套表单值。您可以提供帮助吗me@AnishShah把它作为一个不同的问题发布。肯定会有帮助的@AnishShah请将其作为另一个问题发布。我会帮助你的!我的新问题
def new
  @project = Project.new
  3.times do
    @question = @project.questions.build
  end
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
end

private

def project_params
  params.require(:project).permit(:name,questions_attributes: [:project_id,:subject,:content])       
end
<%= 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 %>
<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>