Q:

Facing error while using render in rails

I am having 2 models:
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 %>

When i use link_to with show path, it is working fine.
app/views/employees/show.html.haml:
 %p
      %strong Title:
      = @employee.full_name
    %p
      %strong Text:
      = @employee.gender
    %p
      = link_to 'Insurance', insurance_path

after accordion code, i used render as follows:
  %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 }

When i am using render in accordion, i am getting error as:
NoMethodError in Employees#show
undefined method `name_of_dependent' for nil:NilClass
Please help me out.

A:

If you are calling show as partial then rename show.html to _show.html
and pass @employeee instance variable which is present in accordion code
= render :template => 'insurances/show', locals: { employee: @employee }

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

