1
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

I'm getting following error

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
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Anubhi Golechha
  • 167
  • 2
  • 14
  • @AnubhiGolechcha, Can you post the error message you're getting? And please format your code. – Arun Kumar Mohan Aug 25 '16 at 04:49
  • 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/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' this is the error I'm getting – Anubhi Golechha Aug 25 '16 at 05:02
  • @AnubhiGolechcha Can you post the url which threw the error? Also update the question with your code in `set_employee_resignation` method and `routes` related to the url. – Arun Kumar Mohan Aug 25 '16 at 05:32
  • URL related to error - " http://localhost:3000/employee_resignations/edit_n_send?resignation_id=58 " – Anubhi Golechha Aug 25 '16 at 07:00

2 Answers2

0

In rails, routes match in priority from top to bottom which means that in your case, the request employee_resignations/edit_n_send?resignation_id=58 is not being sent to employee_resignations#edit_n_send but to employee_resignations#show.

That is why you're getting the error Couldn't find EmployeeResignation with 'id'=edit_n_send). You can confirm this by having a look at the server logs or rake routes.

You need to add the following route before the show route so that edit_n_send is prioritized.

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

If you add the edit_n_send route before the show route, you won't get that error. But there is another problem with your code.

In the controller, you're finding the record based on `params[:resignation_id](1st line). But then, in the third line you're overriding the instance variable with the following code.

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

Since you're just passing resignation_id to the edit action, params[:employee_resignation][:employee_resignation_id] will be nil and it will raise an ActiveRecord::RecordNotFound exception. You should better get rid of that line.

For more, see http://apidock.com/rails/ActiveRecord/FinderMethods/find and Rails 2: Model.find(1) gives ActiveRecord error when id 1 does not exist

Hope this helps.

Community
  • 1
  • 1
Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
  • I had checked all the routes there is no mistake . May be in my model (association) there is some problem . I have three tables Employee Resignation , Reporting Master Resign and Employee History . Can u guide me for the association . Thank You . – Anubhi Golechha Aug 25 '16 at 09:06
  • @AnubhiGolechha Are you getting the same error? If yes, I am sure the problem is with the order of routes. Can you update the question with your routes related to `employee_resignations`? Also the lengthy code in `edit_n_send` isn't relevant. – Arun Kumar Mohan Aug 25 '16 at 09:08
-1

could you see rails's console output or log?

maybe you can see a reqeust log like this

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" => "??"}}

I think it problem to fill 'edit_n_send' value in input field having a name 'resignation_id'

Check a form fields or ajax data in erb file relate with that controller

ogelacinyc
  • 1,272
  • 15
  • 30