2012-01-26 44 views
0

我是新來的編程和新的RoR。飛濺頁面:電子郵件收集(Rails 3.2)

我想建立一個簡單的電子郵件收集窗體的初始頁面,但我收到一個異常,當我試圖在瀏覽器中加載頁面。下面是四個相關文件:

emails_controller.rb

class EmailsController < ApplicationController 
    def new 
    render :layout => false 
    @email = Email.new 
    end 

    def show 
    email = Email.find(params[:id]) 
    end 
end 

email.rb

class Email < ActiveRecord::Base 
    attr_accessible :email 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    validates :email, :presence => true, 
       :format  => { :with => email_regex }, 
       :uniqueness => { :case_sensitive => false } 
end 

的routes.rb

Splashtest::Application.routes.draw do 
    get "emails/new" 

    match 'splash', :to => 'emails#new' 
end 

new.html.erb

<h1>Get notified when we launch:</h1> 

<%= form_for(@email) do |f| %> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

這裏是當我加載http://localhost:3000/splash我得到的錯誤:

未定義的方法`MODEL_NAME」的NilClass:類

請幫助我瞭解爲什麼我在嘗試查看頁面時遇到錯誤?我的目標是將電子郵件地址保存在我的數據庫中。我知道需要更多的代碼才能做到這一點,但我希望先得到這個錯誤。

回答

0
def show 
    @email = Email.find(params[:id]) 
end 

您錯過了@

+0

我錯過了@,但我想清楚它是什麼。我不得不交換render:layout => false和@email = Email.new才能使它工作。 –

0

我不得不交換render:layout => false和@email = Email.new才能使它工作。