I’m making a website with a page where users can sign up to win an iPad. I created a model ipad.rb and an ipads_controller.rb and there were three columns in the migration for name, email and twitter handle. I was surprised when Rails didn’t create routes automatically (I thought it was supposed to do that always).
I added this in the routes file
resource :ipad
match '/signup', :to => 'ipads#new'
When I tried to create the sign up form, I got this error message before fully completing it
undefined method `ipads_path' for #<#<Class:0x00000103a64ec0>:0x00000103a49aa8>
This surprised me because why was it plural?
So far I’ve only created a form in new.html.erb and the model ipad.rb and two actions in the ipads_controller.rb new and create.
Can anyone see what i’m doing wrong? i.e. why does Rails think I need a method ipads_path. Also note that I had created a model IPad.rb but deleted it and the migrations.
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
Your resource ‘ipad’ should be plural in the routes, i.e.