当提交嵌套属性has_many关联表单时,嵌套参数不包括Rails 4中的<name> _attributes

时间:2016-05-22 17:34:11

标签: ruby-on-rails ruby-on-rails-4 nested-forms strong-parameters

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

我发现了这个问题,同样的事情正在发生:
Unpermitted parameter in Rails 4 when submitted nested attributes, has_many associated form
但修改线:

<%= 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/>

如果我可以提供任何其他详细信息,或者我有什么可以尝试的话,请告知我们解决此问题。
谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个:

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

由于只有qustion参数,因此将其视为圆形对象的属性。qustion_attibutes

答案 1 :(得分:0)

试试这个

在您的控制器中

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

答案 2 :(得分:0)

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

你的表格将是,

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

然后使用fields_for,将问题替换为问题,因为回合有很多问题没有问题,

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