ActiveRecord :: RecordNotFound(无法找到带有'id'=的EmployeeResignation

时间:2017-06-01 07:12:40

标签: ruby-on-rails

当我再次点击打印按钮时,它会通过Active Record错误

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

我的控制器代码 -

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

在我写过的路线中:print_exit_interview。

1 个答案:

答案 0 :(得分:0)

是的,这个错误似乎是正确的。您可能没有org.eclipse.e4.workbench.model表中带有空白ID的记录。 employee_resignations可能没有params属性。

如果您想缓解此错误,请改用id。如果在数据库中找不到记录,则EmployeeResignation.find_by(id: params[:id])会引发错误。另一方面,如果未找到具有指定条件的记录,.find()将返回find_by

有一个很好的讨论over here