【问题标题】:Id failure in Ruby On RailsRuby On Rails 中的 ID 失败
【发布时间】:2016-08-25 04:46:49
【问题描述】:
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

【问题讨论】:

  • @AnubhiGolechcha,您可以发布您收到的错误消息吗?请格式化您的代码。
  • ActiveRecord::RecordNotFound (找不到 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/activerecord-4.2.4/lib/active_record/core.rb:155:in find' from /home/vh/workspace/ hrms/app/controllers/employee_resignations_controller.rb:266:in `set_employee_resignation' 这是我得到的错误
  • @AnubhiGolechcha 你能发布引发错误的网址吗?还使用与 url 相关的 set_employee_resignation 方法和 routes 中的代码更新问题。
  • 与错误相关的 URL - "localhost:3000/employee_resignations/…"

标签: ruby-on-rails


【解决方案1】:

在 Rails 中,路由的优先级从 从上到下匹配,这意味着在您的情况下,请求 employee_resignations/edit_n_send?resignation_id=58 不会发送到 employee_resignations#edit_n_send,而是发送到 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

如果您在 show 路由之前添加 edit_n_send 路由,则不会出现该错误。但是您的代码还有另一个问题。

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

 @employee_resignation = EmployeeResignation.find(params[:employee_resignation][:employee_resignation_id])

由于您只是将resignation_id 传递给edit 操作,因此params[:employee_resignation][:employee_resignation_id] 将为nil,它会引发ActiveRecord::RecordNotFound 异常。你最好去掉那条线。

如需了解更多信息,请参阅http://apidock.com/rails/ActiveRecord/FinderMethods/find 和Rails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

希望这会有所帮助。

【讨论】:

  • 我已经检查了所有路线没有错误。可能是我的模型(关联)有一些问题。我有三个表 Employee Resignation、Reporting Master Resign 和 Employee History。你能指导我参加协会吗?谢谢。
  • @AnubhiGolechha 您是否遇到同样的错误?如果是,我确定问题出在路线的顺序上。你能用与employee_resignations 相关的路线更新问题吗? edit_n_send 中的冗长代码也不相关。
【解决方案2】:

你能看到 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 数据

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2014-06-03
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多