[英]undefined method `model_name' for NilClass:Class - ruby on rails application
I have form where a user submit a e-mail, its in the view: views/about/home.html.haml
我有一个表单,用户可以在其中提交电子邮件,其视图如下:
views/about/home.html.haml
The form looks like this: 该表格如下所示:
= form_for @waitinglist do |f|
= f.email_field :email, placeholder: "Enter your email", autofocus: :true
= button_tag class: "btn btn-large btn-block btn-primary sigin-button" do
%p Put me on the waiting list
The controller looks like this: 控制器如下所示:
class WaitinglistsController < ApplicationController
def create
@waitinglist = Waitinglist.new(params[:waitinglist])
if @waitinglist.save
redirect_to root_path
else
render action: :new
end
end
def new
@waitinglist = Waitinglist.new
end
end
The model looks like this: 该模型如下所示:
class Waitinglist < ActiveRecord::Base
attr_accessible :email
validates :email, presence: true
end
But I am getting this error: 但我收到此错误:
undefined method `model_name' for NilClass:Class
What is wrong? 怎么了?
You should add 您应该添加
@waitinglist = Waitinglist.new
in the home
method of your AboutController
. 在
AboutController
的home
方法中。 Your form tries to use @waitinglist
, but if you don't define it it will be nil
and this exception will be thrown. 您的表单尝试使用
@waitinglist
,但是如果您未定义它,它将为nil
并且将抛出此异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.