Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails : add a new route

I'm new with RoR so this is a newbie question:
if I have a controller users_controller.rb and I add a method foo, shouldn't it create this route?

http://www.localhost:3000/users/foo

because when I did that, I got this error:

Couldn't find User with id=foo

I of course added a view foo.html.erb

EDIT:
I added to routes.rb this code but I get the same error:

resources :users do
    get "signup"
  end
like image 903
socksocket Avatar asked Oct 30 '12 18:10

socksocket


People also ask

How do routes work in Ruby on Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

How do you get a Rails route?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

Which file can you set up your routes?

For most applications, you will begin by defining routes in your routes/web.php file. The routes defined in routes/web.php may be accessed by entering the defined route's URL in your browser.


1 Answers

This doesn't work automatically in rails 3. You'll need to add

resource :users do
    get "foo"
end

to your routes.rb

You'll definitely want to have a look at http://guides.rubyonrails.org/routing.html, it explains routing pretty well.

like image 188
Scott S Avatar answered Oct 17 '22 17:10

Scott S