我正在使用has_many关系,其中employee => has_many:保险和保险=> belongs_to:employee
创建功能正常工作但是当我使用编辑时,我得到“ ActionController :: UrlGenerationError in Employees#show “ “显示 /home/raj/Desktop/Projects/empmanagement/app/views/insurances/_show.html.haml第36行: 没有路由匹配{:action =>“edit”,:controller =>“insurances”,:employee_id =>#,:format => nil,:id => nil}缺少必需的键:[:id ] “错误。
routes.rb中:
resources :employees do
resources :insurances
end
这是我的控制者:
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
保险/ _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'
当我运行rake路线时,这就是我得到的:
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
我在Controller更新方法中尝试了以下操作,但没有运气。
@employee = Employee.find(params[:employee_id])
@employee.insurance.update(insurance_params)
redirect_to employee_path(@employee)
请帮帮我
答案 0 :(得分:0)
这应该有效:
link_to 'Edit', [:edit, @employee, @insurance]
答案 1 :(得分:0)
进行以下更改并尝试:
在控制器中:
def new
@employee = Employee.find(params[:employee_id])
@insurance = @employee.insurances.build
end
def edit
@employee = Employee.find(params[:employee_id])
end
def create
@employee = Employee.find(params[:employee_id])
@insurance = @employee.insurances.create(insurance_params)
redirect_to employee_path(@employee)
end
def update
@employee = Employee.find(params[:employee_id])
@insurance.update(insurance_params)
redirect_to employee_path(@employee)
end
并在_show中,将编辑路径更改为:
= link_to "Edit", edit_employee_insurance_path(@employee, employee), :class => 'btn btn-mini btn-primary'