Zack Shapiro
Zack Shapiro

Reputation: 6998

Trying to take emails in a simple field, getting undefined method `signups_path' error

I've got a page which loads at home.html.erb and is controlled by the pages controller.

In the page, I have a form which is a single field that will be used to take email addresses.

The error I'm getting is:

undefined method `signups_path'

Here's my code for your reference, I'm not sure how exactly to define where the route is that it goes.

Home.html.erb contains this form:

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

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

The Pages controller contains:

class PagesController < ApplicationController

  def home
    @title = "Open Domain - Liberate your domains from obscurity"
    @signup = Signup.new
  end

end

The Signup controller contains:

class SignupController < ApplicationController

  def show
    @signup = Signup.new
  end

  def new
  end

  def create
    @signup = Signup.new(params[:signup])
    if @signup.save
    else
      render 'new'
    end
  end

end

And the signup model contains:

class Signup < ActiveRecord::Base

  attr_accessible :email

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

  validates(:email, :presence => true,
                    :length => {:maximum => 40},
                    :format => {:with => email_regex})

end

Any help would be hugely appreciated it. I have a feeling this is a tiny problem and I'm a beginning dev. Thanks!

Upvotes: 0

Views: 130

Answers (2)

miked
miked

Reputation: 4499

The form_for(@signup) is trying to build a route to POST to. If you don't have a named route in your routes.rb, you'll get this error. Try:

routes.rb

 post '/signup', :to=>"signup#create", :as=>"signups"

this basically says: When a POST to the '/signup' path is requested, route it to the create action in the signup controller. Also, make a helper to this path accessible with the name: "signups_path"

Upvotes: 1

Yosep Kim
Yosep Kim

Reputation: 2951

You can replace your form_for tag with this:

<%= form_for @signup, :url => { :action => "create" } do |f| %>

This will post it to "signup/create".

Upvotes: 1

Related Questions