-1

这个例子有点陈词滥调。我正在关注双嵌套表单的 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

然后当我运行许可证时!虽然我得到了这个来测试力的方法:

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

1 回答 1

0

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

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

...在调查下建立协会然后被错误地使用...

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

...建立有问题的答案。我需要更改f变量,所以它应该是这样的:

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

因此,它在调查而不是问题下寻找答案。多谢你们。

于 2013-09-17T19:11:21.427 回答