ActiveRecord::RecordNotFound: Couldn't find Answer with ID=5 for Application with ID=

Viewed 2380

I have a nested form, and am having trouble getting the create and update function to work.

I think what's happening is that the nested object is being created first, and is causing an error.

I get this error when trying to run the create action:

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

Rails.root: /Users/stepan/Desktop/Sites/atlas

Application Trace | Framework Trace | Full Trace
app/controllers/applications_controller.rb:22:in `create'

This only happened after I allowed the :id parameter for the answers model. The reason I did that was to make it possible for the update action to work.

This is what my Controller Looks like ->

class ApplicationsController < ApplicationController


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

    @application = Application.new() 

    @job.questions.count.times do 
         @application.answers.build 
    end 

  end

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

    @application = Application.new(application_params)
    @application.job_id = @job.id

    if @application.save
      redirect_to root_url, :notice => "You have now applied!"
    else
      render :action => 'new'
    end
  end


  def edit 
    @job = Job.find(params[:job_id])
    @user = current_user 
    @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
    @job = Job.find(params[:job_id])
    @user = current_user 
    @application = Application.find(params[:id])

    if @application.update_attributes(application_params)
      redirect_to root_url, :notice => "You have updated your application!"
    else
      render :action => 'new'
    end
  end



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

  def show 
    @job = Job.find(params[:job_id])
    @user = current_user 
    @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 application_params
    params.require(:application).permit(:id, :job_id, :user_id, answers_attributes:[:content, :question_id, :id]).merge(user_id: current_user.id) 
  end

end

This is the form:

<% provide(:title, " Apply to this job") %>

  <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>You're almost done! Answer the questions below and you'll be applied to the job.</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 %>

This is my Application Model:

# == 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
end

This is my answer model:

# == 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

There was a similar problem here --> Rails 4 Nested Attributes Unpermitted Parameters, But I don't quite understand what the answer is doing.

Help much appreciated!

1 Answers
Related