Max Schmeling
Max Schmeling

Reputation: 12509

How do I get a custom action to go to the right place?

I'm new to Ruby on Rails, and I'm sure I'm just missing something simple and stupid, but I can't figure out how to get my 'account/signup' action working.

This is what I have in my routs file:

map.connect ':controller/:action'
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "home"

I've added this to my accounts controller:

def signup
  @account = Account.new

  respond_to do |format|
    format.html # signup.html.erb
    format.xml  { @account }
  end
end

And I've added signup.html.erb to the accounts views folder.

Yet when I go to it in the browser I get this error:

ActiveRecord::RecordNotFound in AccountsController#show Couldn't find Account with ID=signup

What am I doing wrong?

Upvotes: 0

Views: 140

Answers (4)

EmFi
EmFi

Reputation: 23450

The following will do as well when added to the do |map| section of routes.rb

map.resource :account, :member => {:signup => :get}

Will create the standard routes for your accounts controller, as well as add the new route account/signup. It also provides the usual url helpers in addition to signup_account_url and signup_account_path

Upvotes: 0

Mike
Mike

Reputation: 5193

If you want to follow the REST model, you controller should be called sessions and your signup action should be new, so in your routes you could do :

map.resources :sessions

This website is highly recommended to all newcomers to Rails :

http://guides.rubyonrails.org/

Upvotes: 1

JRL
JRL

Reputation: 78033

Here's a tip:

If you run rake routes it'll show you all the possible routes for your app. It should then be obvious depending on what URL you are entering whether it'll be correctly resolved or not.

For a good overview of routes read this guide. There's really a lot of stuff you can do with routes so it's worth taking some time to read it.

Upvotes: 2

Warren Noronha
Warren Noronha

Reputation: 599

Add the following code right on top of your routes.rb file

ActionController::Routing::Routes.draw do |map|
  map.connect 'account/signup', :controller => 'account', :action => 'signup'
  ...
  ...
  ...
end

Also I think you mean Account and not Accounts.

Upvotes: 2

Related Questions