嵌套表单更新操作产生重复结果

时间:2013-12-29 17:36:58

标签: ruby-on-rails nested-forms nested-attributes

在嵌套表单的更新操作期间,它似乎不是更新当前嵌套记录,而是创建新的嵌套记录。

这就是我的控制器的样子:

class ApplicationsController < ApplicationController

  before_filter :set_user_and_job


  def new 
   job = params[:job_id]
   @application = Application.build(job)

  end

  def create
    @application = Application.new(application_params)
    @application.save

    redirect_to root_url, :notice => "You have now applied!"
  end


  def edit 
    @application = Application.find(params[:id])

    @answers = []

    @job.questions.each do |question|
      @application.answers.each do |answer|
        @answers << answer if answer.question_id == question.id
      end
    end

  end

  def update
    @application = Application.find(params[:id])
    @application.update_attributes(application_params)
    redirect_to root_url, :notice => "You have updated your application!"

  end


  def destroy
    Application.find(params[:id]).destroy
    flash[:success] = "Application Deleted."
    redirect_to root_url 
  end 

  def show 
    @application = Application.find(params[:id])

    @answers = []

    @job.questions.each do |question|
      @application.answers.each do |answer|
        @answers << answer if answer.question_id == question.id
      end
    end

  end

private

  def set_user_and_job
      @user = current_user
      @job = Job.find(params[:job_id])
  end

  def application_params
       params.require(:application).permit(:job_id, :user_id, answers_attributes:[:question_id, :content]).merge(user_id: current_user.id, job_id: params[:job_id])
  end


end

这就是我的编辑视图:

<% provide(:title, " Edit this application") %>

  <div class="row">
      <div class="span6">
        <h2> Job: <%= @job.job_title  %></h2>
        <p> <%= @job.job_summary %> </p>
      </div>
      <div class="span6">
        <h2> Applicant: <%= @user.name  %></h2>
      </div>

      <div class="span12">
        <h3>Edit your job application below.</h3>
      </div>
  </div>


<%= form_for [@job, @application] do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

     <% @job.questions.each_with_index do |question| %>
         <%= f.fields_for :answers, question do |question_field| %>
              <%= question_field.label :content, question.content %>
              <%= question_field.text_area :content, :value => "" %>
              <%= question_field.hidden_field :question_id,  :value => question.id  %>
         <% end %>
    <% end %>



   <%= f.submit "Submit the application", class: "button" %>
<% end %>

应用程序模型本身:

# == Schema Information
#
# Table name: applications
#
#  id         :integer          not null, primary key
#  user_id    :integer
#  job_id     :integer
#  created_at :datetime
#  updated_at :datetime
#

class Application < ActiveRecord::Base
    belongs_to :job
    belongs_to :user
    validates :job_id, presence: true 
    validates :user_id, presence: true 
    has_many :answers
    accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true


    def self.build(job_id) 
        application = self.new

        job = Job.find(job_id)
        job.questions.count.times do
         application.answers.build
        end

    application
    end

end

答案模型:

# == Schema Information
#
# Table name: answers
#
#  id             :integer          not null, primary key
#  application_id :integer
#  question_id    :integer
#  created_at     :datetime
#  updated_at     :datetime
#  content        :string(255)
#

class Answer < ActiveRecord::Base
    belongs_to :question
    belongs_to :application
    validates :content, presence: true
end

通过搜索,我找到了这个链接,RoR nested attributes produces duplicates when edit,这表明我将:id添加到application_params,但是当我这样做时,我收到了错误

ActiveRecord::RecordNotFound in ApplicationsController#update
Couldn't find Answer with ID=5 for Application with ID=17

(这也有点奇怪,因为答案的实际ID是39. 5实际上是问题的ID:S)

您对此有何看法? SO的导师,非常感谢:)

2 个答案:

答案 0 :(得分:4)

update_only不适用于has_many关系。您需要将嵌套属性:id字段添加到强参数:

def application_params
   params.require(:application).permit(:job_id, :user_id, 
     answers_attributes:[:id, :question_id, :content]).merge(user_id: current_user.id,
     job_id: params[:job_id])
end

答案 1 :(得分:0)

尝试在update_only的通话中添加accepts_nested_attributes_for

accepts_nested_attributes_for :nested_attribute, update_only: true