1

我有用户提交电子邮件的表单,它在视图中:views/about/home.html.haml

表格如下所示:

 = 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

控制器如下所示:

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

该模型如下所示:

class Waitinglist < ActiveRecord::Base
  attr_accessible :email
  validates :email, presence: true
end

但我收到此错误:

undefined method `model_name' for NilClass:Class

怎么了?

4

1 回答 1

2

你应该添加

@waitinglist = Waitinglist.new

home你的方法中AboutController。您的表单尝试使用@waitinglist,但如果您不定义它,它将nil被抛出,并且将引发此异常。

于 2013-06-26T16:45:18.927 回答