ActionController::UrlGenerationError 在 Employees#show 中有 has_many 关系

ActionController::UrlGenerationError in Employees#show having has_many relation

我正在使用 has_many 关系,其中 employee => has_many :insurances and insurance => 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

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 &nbsp;
  %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'

当我 运行 搜索路由时,这就是我得到的:

                 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

我在控制器更新方法中尝试了以下方法,但没有成功。

 @employee = Employee.find(params[:employee_id])
@employee.insurance.update(insurance_params)
    redirect_to employee_path(@employee) 

请帮帮我

这应该有效:

link_to 'Edit', [:edit, @employee, @insurance]

进行以下更改并尝试:

在控制器中:

 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'