-1

When I click on print button again and again it through an Active Record error

ActiveRecord::RecordNotFound (Couldn't find EmployeeResignation with 'id'=):   app/controllers/exit_interviews_controller.rb:67:in `print_exit_interview'



ActiveRecord::RecordNotFound: Couldn't find EmployeeResignation with 'id'=

My Controller Code -

def print_exit_interview
    @employee = params[:exit_interview] ? params[:exit_interview][:employee_id] : params[:employee_id]
    @employee_resignation = EmployeeResignation.find(params[:id])
    @exit_interviews = ExitInterview.where(employee_id: @employee_resignation.employee_id)
    respond_to do |format|
        format.html
        format.pdf do
        render :pdf => 'print_exit_interview',
        layout: '/layouts/pdf.html.erb',
        :template => 'exit_interviews/print_exit_interview.pdf.erb',
        :orientation      => 'Landscape', # default , Landscape
        :page_height      => 1000,
        :dpi              => '300',
        :margin           => {:top    => 20, # default 10 (mm)
                      :bottom => 20,
                      :left   => 20,
                      :right  => 20},
        :show_as_html => params[:debug].present?
      end
    end
  end

In routes I have written post :print_exit_interview .

Anubhi Golechha
  • 167
  • 2
  • 14

1 Answers1

0

Yes, and that error seems correct. You may not have a record in employee_resignations table with a blank ID. params may not have id attribute in it.

If you want to mitigate this error, use EmployeeResignation.find_by(id: params[:id]) instead. .find() throws an error if record is not found in the database. On the other hand, find_by will return nil if the record is not found with the specified criteria.

A good discussion is available over here.

31piy
  • 23,323
  • 6
  • 47
  • 67
  • now it is not giving any error but when I login with different employee_id it is showing the records of previous employee_id . – Anubhi Golechha Jun 01 '17 at 07:23
  • @AnubhiGolechha It is a separate issue, and there are many possibilities. May be you're passing previously used employee ID in the route. As @Paven mentioned in the comments, please post what did you receive in `params`. – 31piy Jun 01 '17 at 07:27