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

Ste*_*ili 2 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
Run Code Online (Sandbox Code Playgroud)

这是我的编辑视图的样子:

<% 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 %>
Run Code Online (Sandbox Code Playgroud)

应用程序模型本身:

# == 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
Run Code Online (Sandbox Code Playgroud)

和答案模型:

# == 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
Run Code Online (Sandbox Code Playgroud)

通过搜索,我找到了这个链接,RoR 嵌套属性在 edit 时会产生重复项,这表明我将 :id 添加到 application_params,但是当我这样做时,我收到错误

ActiveRecord::RecordNotFound in ApplicationsController#update
Couldn't find Answer with ID=5 for Application with ID=17
Run Code Online (Sandbox Code Playgroud)

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

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

Phi*_*899 5

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
Run Code Online (Sandbox Code Playgroud)