i am using has_many relationship where employee => has_many :insurances and insurance => belongs_to :employee
The create functionality is working fine but when i am using edit, i am getting "ActionController::UrlGenerationError in Employees#show " "Showing /home/raj/Desktop/Projects/empmanagement/app/views/insurances/_show.html.haml where line #36 raised: No route matches {:action=>"edit", :controller=>"insurances", :employee_id=>#, :format=>nil, :id=>nil} missing required keys: [:id]"error.
routes.rb:
resources :employees do
resources :insurances
end
This is my controller:
class InsurancesController < ApplicationController
before_action :set_insurance, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@insurances = Insurance.all
respond_with(@insurances)
end
def show
respond_with(@insurance)
end
def new
@insurance = Insurance.new
respond_with(@insurance)
end
def edit
end
def edit
end
def create
@employee = Employee.find(params[:employee_id])
@insurance = @employee.insurances.create(insurance_params)
redirect_to employee_path(@employee)
end
def update
@insurance.update(insurance_params)
respond_with(@insurance)
end
private
def set_insurance
@insurance = Insurance.find(params[:id])
end
def insurance_params
params.require(:insurance).permit(:name_of_dependent, :relationship, :name, :of_spouse, :children, :date_of_birth, :policy_number, :policy_provider, :policy_type)
end
end
Insurances/_show.html.haml:
%p.pull-right
= flash[:notice]
%br/
= link_to 'Add New Insurances', new_employee_insurance_path(@employee)
%br/
%br/
%table#employee_table.table-bordered.display.dataTable
%thead
%tr
%th Name of Dependent
%th Relationship
%th Name of Spouse
%th Children
%th Date of birth
%th Policy Number
%th Policy provider
%th Policy Type
%th
%tbody
- @employee.insurances.each do |employee|
%tr
%td
= employee.name_of_dependent
%td{:align => "center"}
= employee.relationship
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
%td{:align => "center"}
= link_to "Edit", edit_employee_insurance_path(employee), :class => 'btn btn-mini btn-primary'
When i run rake routes, this is what i am getting:
employee_insurances GET /employees/:employee_id/insurances(.
:format) insurances#index
POST /employees/:employee_id/insurances(.
:format) insurances#create
new_employee_insurance GET /employees/:employee_id/insurances/n
ew(.:format) insurances#new
edit_employee_insurance GET /employees/:employee_id/insurances/:
id/edit(.:format) insurances#edit
employee_insurance GET /employees/:employee_id/insurances/:
id(.:format) insurances#show
PATCH /employees/:employee_id/insurances/:
id(.:format) insurances#update
PUT /employees/:employee_id/insurances/:
id(.:format) insurances#update
DELETE /employees/:employee_id/insurances/:
id(.:format) insurances#destroy
I tried the following in Controller update method but no luck.
@employee = Employee.find(params[:employee_id])
@employee.insurance.update(insurance_params)
redirect_to employee_path(@employee)
Please help me out