Ruby On Rails中的Id失败

时间:2016-08-25 04:46:49

标签: ruby-on-rails

def edit_n_send
    # byebug
    @employee_resignation = EmployeeResignation.find(params[:resignation_id])
    @resignation_history = ResignationHistory.new
    @employee_resignation = EmployeeResignation.find(params[:employee_resignation][:employee_resignation_id])

    @resignation_history.reporting_master_id = @employee_resignation.reporting_master_id
    @resignation_history.resignation_date = @employee_resignation.resignation_date
    @resignation_history.reason = @employee_resignation.reason
    @resignation_history.is_notice_period = @employee_resignation.is_notice_period
    @resignation_history.notice_period = @employee_resignation.notice_period
    @resignation_history.short_notice_period = @employee_resignation.short_notice_period
    @resignation_history.tentative_leaving_date = @employee_resignation.tentative_leaving_date
    @resignation_history.remark = @employee_resignation.remark
    @resignation_history.exit_interview_date = @employee_resignation.exit_interview_date
    @resignation_history.note = @employee_resignation.note
    @resignation_history.leaving_date = @employee_resignation.leaving_date
    @resignation_history.settled_on = @employee_resignation.settled_on
    @resignation_history.has_left = @employee_resignation.has_left
    @resignation_history.notice_served = @employee_resignation.notice_served
    @resignation_history.rehired = @employee_resignation.rehired
    @resignation_history.leaving_reason_id = @employee_resignation.leaving_reason_id
    @resignation_history.employee_resignation_id = @employee_resignation.id

    @employee_resignation.update(employee_id: params[:employee_resignation][:employee_id], reporting_master_id: params[:employee_resignation][:reporting_master_id],leaving_reason_id: params[:employee_resignation][:leaving_reason_id],resignation_date: params[:employee_resignation][:resignation_date],notice_period: params[:employee_resignation][:notice_period],short_notice_period: params[:employee_resignation][:short_notice_period],tentative_leaving_date: params[:employee_resignation][:tentative_leaving_date],remark: params[:employee_resignation][:remark],exit_interview_date: params[:employee_resignation][:exit_interview_date],note: params[:employee_resignation][:note],leaving_date: params[:employee_resignation][:leaving_date],settled_on: params[:employee_resignation][:settled_on],is_stop_pay_request: params[:employee_resignation][:is_stop_pay_request],reason: params[:employee_resignation][:reason],resign_status: "Edit & Send Next")
    ResignationHistory.create(employee_resignation_id: @employee_resignation.id,reporting_master_id: @employee_resignation.reporting_master_id,resignation_date: @employee_resignation.resignation_date,reason: @employee_resignation.reason,is_notice_period: @employee_resignation.is_notice_period,notice_period: @employee_resignation.notice_period,short_notice_period: @employee_resignation.short_notice_period,tentative_leaving_date: @employee_resignation.tentative_leaving_date,remark: @employee_resignation.remark,exit_interview_date: @employee_resignation.exit_interview_date,note: @employee_resignation.note,leaving_date: @employee_resignation.leaving_date,settled_on: @employee_resignation.settled_on,has_left: @employee_resignation.has_left,notice_served: @employee_resignation.notice_served,rehired: @employee_resignation.rehired,leaving_reason_id: @employee_resignation.leaving_reason_id,resign_status: @employee_resignation.resign_status)

    redirect_to resignation_history_employee_resignations_path
    flash[:notice] = ' Request Edited And Send Next Successfully.'   
    ReportingMastersResign.create(reporting_master_id: @employee_resignation.reporting_master_id, employee_resignation_id: @employee_resignation.id, resignation_status: @employee_resignation.resign_status)
    EmployeeResignationMailer.edit_and_send_next(@employee_resignation).deliver_now

    ReportingMastersResign.create(employee_resignation_id: @employee_resignation.id, reporting_master_id: params[:employee_resignation][:reporting_master_id], resignation_status: "Edit & Send Next")
end

我收到了以下错误

ActiveRecord::RecordNotFound (Couldn't find EmployeeResignation with 'id'=edit_n_send): 
app/controllers/employee_resignations_controller.rb:266:in set_employee_resignation' 
ActiveRecord::RecordNotFound: Couldn't find EmployeeResignation with 'id'=edit_n_send 
from /home/vh/.rvm/gems/ruby-2.2.1/gems&#‌​47;activerecord-4.2.‌​4/lib/active‌​_record/core.rb:‌​155:in find' 
from /home/vh/workspace/hrms/app/controllers/employee_resignation‌​s_controller.rb:266:‌​in `set_employee_resignation

2 个答案:

答案 0 :(得分:0)

在rails中,路由优先级从从上到下匹配,这意味着在您的情况下,请求employee_resignations/edit_n_send?resignation_id=58不会发送到employee_resignations#edit_n_send而是发送到{{1 }}。

这就是您收到错误employee_resignations#show的原因。您可以通过查看服务器日志或Couldn't find EmployeeResignation with 'id'=edit_n_send)确认这一点。

您需要在rake routes路由之前添加以下路由,以便show具有优先级。

edit_n_send

如果您在get '/employee_resignations/edit_n_send' => 'employee_resignations#edit_n_send' # show route should go next to the above route resources :employee_resignations # I'm assuming you have this defined 路线之前添加edit_n_send路线,则不会收到该错误。但是你的代码还有另一个问题。

在控制器中,您将根据`params [:resignation_id](第1行)找到记录。但是,在第三行中,您将使用以下代码覆盖实例变量。

show

由于您只是将 @employee_resignation = EmployeeResignation.find(params[:employee_resignation][:employee_resignation_id]) 传递给resignation_id操作,edit将为零,并且会引发params[:employee_resignation][:employee_resignation_id]例外。你应该更好地摆脱那条线。

有关详情,请参阅http://apidock.com/rails/ActiveRecord/FinderMethods/findRails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

希望这有帮助。

答案 1 :(得分:-1)

你能看到rails的控制台输出还是日志?

也许你可以看到像这样的需求日志

Started GET "/edit_n_send" for ::1 at 2016-08-25 14:47:05 +0900 Processing by EmployeeResignationsController#set_employee_resignation as HTML Parameters: {"resignation_id"=>"edit_n_send", "employee_resignation" => {"employee_resignation_id" => "??"}}

我认为在名为'resignation_id'的输入字段中填写'edit_n_send'值有问题

检查erb文件中的表单字段或ajax数据与该控制器的关联