嵌套参数不包含<name>_attributes in Rails 4 提交嵌套属性时,has_many关联表单

Nested parameter does not include <name>_attributes in Rails 4 when submitted nested attributes, has_many associated form

在我的 Rails 应用程序 (v.4.2.6) 中,当尝试将问题表单嵌套在回合表单中时,
我收到一个 Unpermitted parameter 错误。
我可以看到 Rails 没有将我的参数从问题转换为 questions_attributes。

我发现了这个问题,其中发生了同样的事情:

但修改行:

<%= f.fields_for :questions do |builder| %>  

<%= f.fields_for :questions, @question do |builder| %>  

没有解决我的问题。

控制台输出(参数):

Processing by RoundsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"oc5BHCv7pQzrKmcPG9x5nu+xMyOAJ/s09uiLakRrSLv6P5hND/xNKYn9qOyelSWK4MmqceZcdsESAWJ40coOgg==", "round"=>{"title"=>"Test", "questions"=>{"questiontext"=>"Q", "answertext"=>"A", "partialanswer"=>"", "difficulty"=>"", "pointval"=>"", "partialpoints"=>"", "notes"=>""}}, "commit"=>"Save Round"}
Unpermitted parameter: questions

这是我的代码:

型号:

class Round < ActiveRecord::Base
    has_many :questions
    accepts_nested_attributes_for :questions, allow_destroy: true
end

class Question < ActiveRecord::Base
    belongs_to :round
end

圆形控制器:

class RoundsController < ApplicationController

    def index
        @rounds = Round.all
    end

    def new
        @round = Round.new
        @round.questions.build
        @questions = Question.all 
        @question = Question.new 
    end

    def create
        @round = Round.new(round_params)
        if @round.save 
            redirect_to @round
        else 
            render 'new'
        end 

    end

    private
        def round_params
            params.require(:round).permit(:title, questions_attributes: [:id, :questiontext, :answertext])
        end


end

表格:

<%= form_for :round, url: rounds_path do |f| %> 

<p> 
<%= f.label :title %> 
<%= f.text_area :title %> 
</p>

<H4> Add Questions to Round </H4>

    <%= f.fields_for :questions, @question do |builder| %>  

                <%= render "question_fields", :f => builder %>

    <% end %>

<%= f.submit %>

<% end %> 

部分:

<%= f.label :questiontext, "Question" %><br/>
<%= f.text_area :questiontext %><br/>

<%= f.label :answertext, "Answer" %><br/>
<%= f.text_area :answertext %><br/>

请告诉我是否可以包含任何其他详细信息,或者是否有任何我可以尝试解决此问题的方法。
谢谢。

试试这个:

 <%= nested_form_for :round, url: rounds_path do |f| %> 

因为只有 qustion 个参数,所以它认为它是圆形对象的属性。qustion_attibutes

试试这个

在你的控制器中

class RoundsController < ApplicationController

  def index
    @rounds = Round.all
  end

  def new
    @round = Round.new
  end

  def create
    @round = Round.new(round_params)
    @question = @round.questions.new(questiontext: params[:question][:questiontext], answertext: params[:question][:answertext])
    @question.save
    if @round.save 
        redirect_to @round
    else 
        render 'new'
    end
  end

  private
    def round_params
        params.require(:round).permit(:title, questions_attributes: [:id, :questiontext, :answertext])
    end
end

现在在你的表单中

<%= form_for :round, url: rounds_path do |f| %> 

    <p> 
       <%= f.label :title %> 
       <%= f.text_area :title %> 
    </p>

    <H4> Add Questions to Round </H4>      

    <%= render "question_fields", locals: {:f => builder} %>

    <%= f.submit %>

<% end %> 

现在在你的部分

<%= f.fields_for :question do |q| %> 
   <%= q.label :questiontext, "Question" %><br/>
   <%= q.text_area :questiontext %><br/>

   <%= q.label :answertext, "Answer" %><br/>
   <%= q.text_area :answertext %><br/>
<% end %> 

使用 nested_form_for 辅助方法启用嵌套。

您的表单将是,

<%= nested_form_for @round do |f| %>

然后用fields_for,把question换成questions因为round有很多问题不是question,

<%= f.fields_for :questions %>

现在在你的部分,

<%= f.label :questiontext, "Question" %><br/>
<%= f.text_area :questiontext %><br/>

<%= f.label :answertext, "Answer" %><br/>

<%= f.text_area :answertext %><br/>

参考nested form