accepts_nested_attributes_for - 为什么我的表单从我的嵌套模型中删除实例?

时间:2013-05-12 14:00:49

标签: ruby-on-rails nested-attributes

我有......

/app/models/survey.rb

class Survey < ActiveRecord::Base

  has_many :results
  belongs_to :competitor
  after_commit :create_matching_results  
  attr_accessible :results_attributes
  accepts_nested_attributes_for :results
  after_commit :create_matching_results
  def create_matching_results
    unless self.destroyed?
      self.fields.each do |field|
        result = survey.results.find_or_create_by_survey_id_and_field_id(self.id, field_id)
        result.update_attributes :competitor_id => competitor.id
      end
    end
  end
end

/app/models/result.rb

class Result < ActiveRecord::Base
  attr_accessible :field_id, :survey_id, :competitor_id, :value, :selected, :name
  belongs_to :survey
end

/app/controllers/surveys_controller.rb

class SurveysController < ApplicationController  
  def edit
    @competitor = Competitor.find(params[:competitor_id])
    @survey = @competitor.surveys.find(params[:id])
  end
  def update
    @competitor = Competitor.find(params[:competitor_id])
    @survey = @competitor.surveys.find(params[:id])
    @result_ids = params[:survey]["result_ids"]
    @survey.results.each do |result|
      result.update_attributes :selected => true if result.id == @result_ids.to_i
      result.update_attributes :selected => false unless result.id == @result_ids.to_i
    end
    flash[:notice] = "Survey was successfully updated. (#{undo_link(@survey)}.)" if @survey.update_attributes(params[:survey])
    respond_with(@competitor, @survey)
  end
end

/app/views/surveys/_form.html.haml

= simple_form_for [@competitor, @survey] do |f|
  = f.collection_radio_buttons :result_ids, @survey.results, :id, :name
  = f.button :submit, :id => 'submit_survey'

现在让我们假设@ survey.results.count == 2.然后,我会在我的表单中得到一些可爱的HTML:

<form accept-charset="UTF-8" action="/competitors/13/surveys/80" class="simple_form edit_survey" id="edit_survey_80" method="post" novalidate="novalidate">
  <input id="survey_result_ids_673" name="survey[result_ids]" type="radio" value="673" />
  <label class="collection_radio_buttons" for="survey_result_ids_673">one</label>
  <input id="survey_result_ids_672" name="survey[result_ids]" type="radio" value="672" />
  <label class="collection_radio_buttons" for="survey_result_ids_672">two</label>
  <input class="btn" id="submit_survey" name="commit" type="submit" value="Update Survey" />
</form>

到目前为止,非常好。

当我选择这两个单选按钮中的一个时,我的参数看起来像这样:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"etc...",
 "survey"=>{"result_ids"=>"672"},
 "commit"=>"Update Survey",
 "action"=>"update",
 "controller"=>"surveys",
 "competitor_id"=>"13",
 "id"=>"80"}

因此,我希望将selected的{​​{1}}值设置为Result.find(672),将另一个结果的true值设置为{{1} }}。相反,我发现其他结果完全被删除了。这是怎么回事?我该如何解决?

更新

例如,我从selected开始,false。然后我在Rspec中执行以下操作:

@survey.results.count == 4
然后我得到:

results.selected == false

(并不是说我希望'before'和'after'值相等。这个技巧只是在Rspec测试失败消息中显示这两个值的一种方便方法。)

1 个答案:

答案 0 :(得分:0)

答案是删除accepts_nested_attributes_for,因为我已经直接设置了结果值。

class Survey < ActiveRecord::Base
  belongs_to :competitor
  attr_accessible :selection
  attr_accessor :selection
  has_many :results
  belongs_to :question
end

class SurveysController < ApplicationController
  def update
    @competitor = Competitor.find(params[:competitor_id])
    @survey = @competitor.surveys.find(params[:id])
    @result_ids = params[:survey]["selection"]
    @survey.results.each do |result|
      result.update_attributes :selected => true if result.id == @result_ids.to_i
      result.update_attributes :selected => false unless result.id == @result_ids.to_i
    end
    flash[:notice] = "Survey was successfully updated. (#{undo_link(@survey)}.)" if @survey.update_attributes(params[:survey])
    respond_with(@competitor, @survey)
  end  
end

= simple_form_for [@competitor, @survey] do |f|
  = f.collection_radio_buttons :selection, @survey.results, :id, :name
  = f.button :submit, :id => 'submit_survey'