Fields_for has_many关联不保存

时间:2013-06-07 00:28:28

标签: ruby-on-rails activerecord associations fields-for

所以我在民意调查,问题和答案中有一个深层次的嵌套关联:

class Poll < ActiveRecord::Base
  attr_accessible :description, :end_time, :start_time, :title, :user_id, :questions_attributes, :is_live

  belongs_to :user
  has_many :questions, :dependent => :destroy
  # This is the plularized form of sms, it's not smss
  has_many :sms, :through => :questions 
  has_many :feedbacks

  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
end

class Question < ActiveRecord::Base
  attr_accessible :poll_id, :title, :answer_attributes

  belongs_to :poll
  has_many :answers, :dependent => :destroy
  # THis is the plularized form of sms, it's not smss
  has_many :sms
                                          #If someone creates a form with a blank question field at the end, this will prevent it from being rendered on teh show page
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true

end

class Answer < ActiveRecord::Base
  attr_accessible :is_correct, :question_id, :title

  belongs_to :question
end

我正在尝试将所有对象保存在单个fields_for表单中。

= form_for @poll, :class=>'create-poll-form' do |f|
  = f.text_field :title, :autofocus => true, :placeholder => "Poll Title"
  = f.text_field :description, :placeholder => 'Description'  
  = f.fields_for :questions do |builder| 
    = render "questions/question_fields", :f => builder

  = f.submit "Create Poll", :class => 'btn btn-danger'

问题部分

%p
  = f.label :title, "Question"
  = f.text_field :title
  = f.check_box :_destroy
  = f.label :_destroy, "Remove Question"
%p
  = f.fields_for :answers do |builder|
    = render "answers/answer_fields", :f => builder

回答部分

%p
  = f.label :title, "Answer"
  = f.text_field :title
  = f.check_box :_destroy
  = f.label :_destroy, "Remove Answer"

然而,PollsController并未持久保存数据:

  def create
    binding.pry
    @poll = current_user.polls.new(params[:poll])
    # @poll = Poll.create(params[:poll])
    binding.pry
    @poll.save!
    redirect_to root_path
  end

  def new
    @poll = Poll.new  

    1.times do
      question = @poll.questions.build
      2.times {question.answers.build}
    end
  end

这里的任何提示都会很棒,我一直在研究这个问题,这让我很难受!提前谢谢!

此处还有服务器日志:

Started POST "/polls" for 127.0.0.1 at 2013-06-06 20:14:47 -0400
Processing by PollsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hk+KuNsTLx3sH9pE7Mf8XETGsmxTsRN4/tWUBn3CIVE=", "poll"=>{"title"=>"Testing 1-2-1-2", "description"=>"Up on the mic", "questions_attributes"=>{"0"=>{"title"=>"Cake or Death?", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"title"=>"Cake", "_destroy"=>"0"}, "1"=>{"title"=>"Death", "_destroy"=>"0"}}}}}, "commit"=>"Create Poll"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  Question Load (0.5ms)  SELECT "questions".* FROM "questions" WHERE "questions"."poll_id" IS NULL
   (0.3ms)  BEGIN
  SQL (47.6ms)  INSERT INTO "polls" ("created_at", "description", "end_time", "is_live", "start_time", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["created_at", Fri, 07 Jun 2013 00:16:46 UTC +00:00], ["description", "Up on the mic"], ["end_time", nil], ["is_live", nil], ["start_time", nil], ["title", "Testing 1-2-1-2"], ["updated_at", Fri, 07 Jun 2013 00:16:46 UTC +00:00], ["user_id", 1]]
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 170668ms (ActiveRecord: 54.9ms)

1 个答案:

答案 0 :(得分:2)

我不确定这是否是问题,但在你的

class Question < ActiveRecord::Base
  attr_accessible :poll_id, :title, :answer_attributes

但是在你的日志文件中它有

Parameters: {"utf8"=>"✓", "authenticity_token"=>"hk+KuNsTLx3sH9pE7Mf8XETGsmxTsRN4/tWUBn3CIVE=", "poll"=>{"title"=>"Testing 1-2-1-2", "description"=>"Up on the mic", "questions_attributes"=>{"0"=>{"title"=>"Cake or Death?", "_destroy"=>"0", "answers_attributes"=>{"0"=>{"title"=>"Cake", "_destroy"=>"0"}, "1"=>{"title"=>"Death", "_destroy"=>"0"}}}}}, "commit"=>"Create Poll"}

差异为:answers_attributes,而不是:answer_attributes,即回答与回答* s

这可能会导致数据被丢弃。