在另一个控制器视图中调用表单时 NilClass:Class 的未定义方法“model_name”
undefined method `model_name' for NilClass:Class when calling form in another controllers view
我有 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
@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
app/views/insurances/_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
app/views/employees/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 中,当我呈现 insurances/show 时,显示条件工作正常,当没有记录时,不显示创建表单,它列出错误如下:
NilClass:Class
的未定义方法“model_name”
我尝试使用 = simple_form_for([:employee,Insurance.new], :url =>{:controller => "insurances", :action => :create)
do |f|但这并没有解决我的问题。
在部分内部使用局部变量:employee
而不是 @employee
。
当你渲染一个partial并使用locals:
传入一个变量时,它实际上是作为局部变量传入的。同名的实例变量将为零。
我有 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
@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
app/views/insurances/_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
app/views/employees/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 中,当我呈现 insurances/show 时,显示条件工作正常,当没有记录时,不显示创建表单,它列出错误如下:
NilClass:Class
的未定义方法“model_name”我尝试使用 = simple_form_for([:employee,Insurance.new], :url =>{:controller => "insurances", :action => :create)
do |f|但这并没有解决我的问题。
在部分内部使用局部变量:employee
而不是 @employee
。
当你渲染一个partial并使用locals:
传入一个变量时,它实际上是作为局部变量传入的。同名的实例变量将为零。