在 rails 中使用渲染时遇到错误

Facing error while using render in rails

我有 2 个模型:

app/models/employee.rb:

class Employee < User
    has_many :insurances 
end

app/models/insurance.rb:

 class Insurance < ActiveRecord::Base
        belongs_to :employee
    end

app/controllers/employees_controller.rb:

class EmployeesController < ApplicationController
  before_action :set_employee, only: [:show, :edit, :update, :destroy]
  before_action :employee_params, only: [:create, :update]
  # GET /employees
  # GET /employees.json
  def index
    @employees = Employee.all
  end

  # GET /employees/1
  # GET /employees/1.json

  def show
    @employee = Employee.find(params[:id])
  end

  # GET /employees/new
  def new
    @employee = Employee.new
  end

  # GET /employees/1/edit
  def edit
     @employee = Employee.find(params[:id])
  end

  # POST /employees
  # POST /employees.json
  def create
    @employee = Employee.new(employee_params)

    respond_to do |format|
      if @employee.save
        format.html { redirect_to employees_url, notice: "#{@employee.first_name} was successfully created." }
        format.json { render :index, status: :created, location: @employee }
      else
        format.html { render :new }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /employees/1
  # PATCH/PUT /employees/1.json
  def update
    respond_to do |format|
      if @employee.update(employee_params)
        format.html { redirect_to employees_url, notice: "#{@employee.first_name} was successfully updated."}
        format.json { render :index, status: :ok, location: @employee }
      else
        format.html { render :edit }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /employees/1
  # DELETE /employees/1.json
  def destroy
    @employee.destroy
    respond_to do |format|
      format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_employee
      @employee = Employee.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def employee_params
      if params[:employee][:password].blank? && params[:employee][:password_confirmation].blank?
          params[:employee].delete(:password)
          params[:employee].delete(:password_confirmation)
      end
      params[:employee].permit(:email, :password, :employee_id,:employee_work_id, :first_name, :middle_name, :last_name, :gender, :date_of_birth, :driver_license_no, :driver_license_expiry_date, :martial_status, :nationality, :office_address, :residence_address, :city, :state_province, :zip_code, :country, :work_number, :mobile_number, :home_number, :other_email)

    end
end

app/controllers/insurance_controller.rb:

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 create
        @insurance = Insurance.new(insurance_params)
        @insurance.save
        respond_with(@insurance)
      end

      def update
        @insurance.update(insurance_params)
        respond_with(@insurance)
      end

      def destroy
        @insurance.destroy
        respond_with(@insurance)
      end

      private
        def set_insurance
          @insurance = Insurance.find(params[:id])
        end

        def insurance_params
          params.require(:insurance).permit(:employee_id,:name_of_dependent, :relationship, :name, :of_spouse, :children, :date_of_birth, :policy_number, :policy_provider, :policy_type)
        end
    end

app/views/insurances/_show.html.erb:

    <%= employee.insurances.each do |emp| %>
    <p>
      <strong>Name of dependent:</strong>
      <%= emp.name_of_dependent %>
    </p>

    <p>
      <strong>Name:</strong>
      <%= emp.name %>
    </p>
<% end %>

当我将 link_to 与显示路径一起使用时,它工作正常。

app/views/employees/show.html.haml:

 %p
      %strong Title:
      = @employee.full_name
    %p
      %strong Text:
      = @employee.gender
    %p
      = link_to 'Insurance', insurance_path

在手风琴代码之后,我使用渲染如下:

  %p
      %strong Title:
      = @employee.full_name
    %p
      %strong Text:
      = @employee.gender
    %p
  #accordion2.panel-group
    #new-student-widget.panel.panel-default.left-column-entry
      .header.panel-heading
        .header-content.panel-title
          %a#newStudentToggle{"data-parent" => "#accordion2", "data-target" => "#newStudent", "data-toggle" => "collapse"} Insurance Details
      #newStudent.panel-collapse.collapse
        #newStudentInner.panel-body
          = render :template => 'insurances/show', locals: { employee: @employee }

当我在手风琴中使用渲染器时,出现以下错误: 员工中没有方法错误#show nil:NilClass 的未定义方法“name_of_dependent” 请帮帮我。

渲染模板时传递 insurance 对象:

- @insurances.each do |insurancce|
  = render template: 'insurances/show', locals: { insurance: insurance }
- end

确保在控制器操作中实例化了 @insurance 对象,在那里实例化了 @employee 对象。

@employee = current_user # or anything else
@insurances = @employee.insurances

然后在您的 Insurances#show 模板中使用 insurance 而不是 @insurance,如下所示:

<p id="notice"><%= notice %></p>
<p>
  <strong>Name of dependent:</strong>
  <%= insurance.name_of_dependent %>
</p>

<p>
  <strong>Name:</strong>
  <%= insurance.name %>
</p>

如果您调用 show as partial 则将 show.html 重命名为 _show.html 并传递手风琴代码中存在的@employeee实例变量

= render :template => 'insurances/show', locals: { employee: @employee }

并在 _show 页面中

- employee.insurances.each do |ins|
    %p.strong Name of dependent:
    = ins.name_of_dependent
    %p.strong Name:
    = ins.name