我有2个型号:
应用程序/模型/ employee.rb:
class Employee < User
has_many :insurances
end
app / models / insurance.rb:
class Insurance < ActiveRecord::Base
belongs_to :employee
end
应用程序/控制器/ 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
应用程序/控制器/ 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
@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
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(:name_of_dependent, :relationship, :name, :of_spouse, :children, :date_of_birth, :policy_number, :policy_provider, :policy_type)
end
end
应用程序/视图/保险/ _show.html.erb:
- if @employee.insurances.exists?
- @employee.insurances.each do |emp|
%p
%strong Name of dependent:
= emp.name_of_dependent
%p
%strong Relationship:
= emp.relationship
%p
%strong Name of spouse:
= emp.name_of_spouse
%p
%strong Children:
= emp.children
%p
%strong Date of birth:
= emp.date_of_birth
%p
%strong Policy number:
= emp.policy_number
%p
%strong Policy provider:
= emp.policy_provider
%p
%strong Policy type:
= emp.policy_type
- else
= simple_form_for([:employee,@insurance]) do |f|
= f.error_notification
.form-inputs
= f.input :name_of_dependent
= f.input :relationship
= f.input :name_of_spouse
= f.input :children
= f.input :date_of_birth
= f.input :policy_number
= f.input :policy_provider
= f.input :policy_type
.form-actions
= f.button :submit
应用程序/视图/雇员/ show.html.haml:
%p
%strong Title:
= @employee.full_name
%p
%strong Text:
= @employee.gender
%p
%strong Insurance Details:
= render :partial => 'insurances/show', locals: { employee: @employee }
在employees / show中,当我呈现保险/节目时,节目状态正常,当没有记录时,创建表单不会显示,并且列出错误为:
NilClass的未定义方法`model_name':Class
我尝试使用= simple_form_for([:employee,Insurance.new], :url =>{:controller => "insurances", :action => :create)
执行| f |但它没有解决我的问题。
答案 0 :(得分:1)
在partials中使用局部变量:employee
而不是@employee
。
当渲染部分并使用locals:
传入变量时,它实际上是作为局部变量传入的。同名的实例变量将为nil。