我正在制作一个网页,其中包含一个用户可以注册赢取iPad的页面。我创建了一个模型ipad.rb
和一个ipads_controller.rb
,并且在名称,电子邮件和Twitter处理的迁移中有三列。当Rails没有自动创建路由时我感到很惊讶(我认为它应该始终这样做)。
我在路线文件中添加了这个
resource :ipad
match '/signup', :to => 'ipads#new'
当我尝试创建注册表单时,在完全填写之前收到此错误消息
undefined method `ipads_path' for #<#<Class:0x00000103a64ec0>:0x00000103a49aa8>
这令我感到惊讶,因为它为何复数?
到目前为止,我只在new.html.erb
和模型ipad.rb
中创建了一个表单,并在ipads_controller.rb
new
和create
中创建了两个操作。
谁能看到我做错了什么?即为什么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+\-.]+@[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 :(得分:2)
您的资源&#39; ipad&#39;在路线中应该是复数,即
资源:ipads