具有强参数的嵌套表单; object_attribute方法不可用?

时间:2013-09-12 08:09:45

标签: ruby-on-rails ruby strong-parameters

这个例子有点陈词滥调。我正在关注有关双嵌套表单的railscasts第196集(http://railscasts.com/episodes/196-nested-model-form-revised?autoplay=true)。

我得到的错误是我的 accepts_nested_attributes_for 命令没有为强参数生成 answers_attributes

型号:

class Survey < ActiveRecord::Base
  has_many :questions
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers
  has_many :users, through: :answers
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :user
end

然后我的调查控制员:

class SurveysController < ApplicationController

  def index
    @survey = current_user.surveys
  end

 def new
   @survey = Survey.new
   @question = @survey.questions.build   # the nested form won't show up if I don't
   @answer = @question.answers.build     #Not sure if I need this line. doesn't work either way.
 end

 def create
   @survey = Survey.new(survey_params)
   if @survey.save
     redirect_to @survey, notice: "Survey successfully created."
   else
     render 'new'
   end
 end

# rest.. show, edit, update, destroy


private

def survey_params
  params.require(:survey).permit! 
end

#def survey_params
  #params.require(:survey).permit(:user_id, :name, { questions_attributes: [:_destroy, :id, :survey_id, :content, { answers_attributes: [:_destroy, :id, :content, :user_id, :question_id]}]})
 #end 
end

我遇到的问题是这个(空表单提交)。当我运行我的真实代码时,我得到了这个:

{"utf8"=>"✓", "authenticity_token"=>"[token]=", "survey"=>{"user_id"=>"1", "name"=>"test", "questions_attributes"=>{"0"=>{"content"=>"test", "_destroy"=>"0"}}, "answers"=>{"content"=>"test", "_destroy"=>"0"}}, "commit"=>"Create", "action"=>"create", "controller"=>"surveys"}
Unpermitted attributes: answers

然后,当我运行 permit!方法测试力时,我得到了这个:

unknown attribute: answers

Rails应该寻找answers_attributes而不是答案。这让我觉得它没有认识到这个模型。所以,这是架构。

create_table "questions", force: true do |t|
  t.integer  "survey_id"
  t.string   "content"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "answers", force: true do |t|
  t.integer  "question_id"
  t.string   "content"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "user_id"
end

如果您对如何修复我的错误以某种方式告诉rails寻找答案而不是answers_attributes,请告诉我。

更新:这是我的表单。

<%= form_for @survey do |f| %>
<%= f.label :user_id %>


<%= f.collection_select :user_id, User.all, :id, :email, {}, { :multiple => false } %><br>
<%= f.text_field :name, placeholder: "Survey name"%>


<%= f.fields_for :questions do |builder| %>
  <%= builder.label :content, "Question 1"%>
  <%= builder.text_area :content %>
  <%= builder.check_box :_destroy %>
  <%= builder.label :_destroy, "Remove Question" %>

  <%= f.fields_for :answers do |builder| %>
    <%= builder.text_field :content, placeholder: "Answer 1" %>
    <%= builder.check_box :_destroy %>
  <% end %>
<% end %><br>

<%= f.submit "Create", :class => "btn btn-large btn-warning"  %>
<% end %>

1 个答案:

答案 0 :(得分:0)

解决方案是对表格的疏忽。我用过......

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

......根据调查建立协会,然后错误地使用......

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

......构建有问题的答案。我需要更改f变量,所以它肩负这样的事情:

<%= builder.fields_for :answers do |x| %>

所以,它正在寻找调查中的答案而不是问题。谢谢你们。