2012-01-08 95 views
0

我正在制作一个网页,其中有一个用户可以注册以赢得iPad的页面。我创建了一个模型ipad.rb和一个ipads_controller.rb,并且在迁移中有三列用于名称,电子邮件和推特处理。当Rails没有自动创建路由时,我感到很惊讶(我认为它应该始终这样做)。Rails:`ipads_path'与iPad.rb模型和ipads_session控制器的错误消息

我的路线添加文件

resource :ipad 
    match '/signup', :to => 'ipads#new' 

当我试图创建注册表单,我完全完成它

undefined method `ipads_path' for #<#<Class:0x00000103a64ec0>:0x00000103a49aa8> 

这让我感到惊讶,因为为什么是之前得到这个错误消息它是复数?

到目前为止,我只创建了一个new.html.erb形式和模型ipad.rb并在ipads_controller.rbnewcreate两个动作。

任何人都可以看到我做错了什么?即为什么Rails认为我需要一种方法ipads_path。另外请注意,我创建了一个模型IPad.rb,但删除了它和迁移。

new.html.erb

<h1>Win an iPad</h1> 

<h1>Sign up for iPad</h1> 

<%= form_for(@ipad) do |f| %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

ipad.rb

class Ipad < ActiveRecord::Base 

attr_accessible :name, :email, :twitter 

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

validates :name, :presence => true, 
       :length => { :maximum => 50 } 
validates :email, :presence => true, 
        :format => { :with => email_regex }, 
        :uniqueness => true 

end 

ipads_controller.rb

class IpadsController < ApplicationController 

    def new 
    @ipad = Ipad.new 
    @title = "iPad Contest" 
    end 

    def create 
    @ipad = Ipad.new(params[:ipad]) 
    if @ipad.save 
     # Handle a successful save. 
    else 
     @title = "iPad Contest" 
     render 'new' 
    end 
    end 


end 

的routes.rb

Enki::Application.routes.draw do 
    namespace 'admin' do 
    resource :session 

    resources :posts, :pages do 
     post 'preview', :on => :collection 
    end 
    resources :comments 
    resources :undo_items do 
     post 'undo', :on => :member 
    end 

    match 'health(/:action)' => 'health', :action => 'index', :as => :health 



    root :to => 'dashboard#show' 
    end 

    resources :archives, :only => [:index] 
    resources :pages, :only => [:show] 

    resource :ipad 
    match '/signup', :to => 'ipads#new' 

    constraints :year => /\d{4}/, :month => /\d{2}/, :day => /\d{2}/ do 
    get ':year/:month/:day/:slug/comments' => 'comments#index' 
    post ':year/:month/:day/:slug/comments' => 'comments#create' 
    get ':year/:month/:day/:slug/comments/new' => 'comments#new' 
    get ':year/:month/:day/:slug' => 'posts#show' 
    end 

    scope :to => 'posts#index' do 
    get 'posts.:format', :as => :formatted_posts 
    get '(:tag)', :as => :posts 
    end 

    root :to => 'posts#index' 
end 
+0

我觉得你的模型的选择很可能得到改善。尽管你所做的并不是“错误的”,但确切地说,它不是特别明确的代码,也不可能是非常可重用的。我会考虑对它进行重构,以便您拥有一个与它关联的Entry模型的Contest模型。看起来很奇怪,有一个名为IPad的模型映射到用户信息表。 – 2012-01-09 01:33:58

+0

我同意。我是小白。这可能是我第二次创建了一个模型(除了一本书以外)。 – Leahcim 2012-01-09 01:39:24

回答

2

你的资源 '的iPad' 应该是在航线复数,即

资源:解锁iPhone和iPad

+0

谢谢,如果你有时间可以解释为什么?例如,有'会话'的资源,但它是单数的...... – Leahcim 2012-01-09 00:39:08

+1

如果你的控制器是'iPadController'('ipad_controller.rb'),那么你会使用单一的资源名称。相反,你有'iPadsController',所以它正在寻找一个复数声明。 – iwasrobbed 2012-01-09 02:19:16