Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails rake routes where are they from

When you type

 rake routes 

a bunch of routes come out, but where are they defined???

I know some are default, and how about the others?

For example, this is a script from a controller, I tried to take off the 's' from do_something, but can't make it work.... are they defined somewhere else too? Also, when do they take parameters and when not, how I know it ? Thanks!

def hello
  redirect_to do_things_shop_path(shop)
end

def do_things
end
like image 812
runcode Avatar asked Nov 15 '12 03:11

runcode


People also ask

What are Rails resource routes?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.

How do Rails routes work?

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 I find routes in Rails?

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.

How do I get a list of Rake tasks?

You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks . Each task has a description, and should help you find the thing you need.


1 Answers

Rails routing configurations are kept in config/routes.rb file.

Taking parameters depends on many things. rake routes will show with routes take parameters. Member actions will take parameters.

posts    GET        /posts(.:format)          posts#index
         POST       /posts(.:format)          posts#create
edit_post GET       /posts/:id/edit(.:format) posts#edit

In the last line, you will url like posts/:id/edit. This path requires :id parameter. You can call this route many ways. One of them is like:

edit_post_path(@post)

If you want to create a custom action, (say under posts controller), you can declare it as follow:

match `/posts/:id/things_with_id`, :to => 'posts#do_things_with_id', :as => 'do_things_with_id
match `/posts/things_without_id`, :to => 'posts#do_things_without_id', :as => 'do_things_without_id

First one requires an ID while the second one does not. Call them accordingly:

do_things_with_id_path(@post)

do_things_without_id()

For a resource, you can create these easily using member & collection action. Member action needs id while collection action does not.

resources :posts do 
  member { get 'do_thing' }
  collection { get do_things' }
end

hope you got it.

By the way, you must read the following guide if you want to understand these clearly. http://guides.rubyonrails.org/routing.html

like image 66
HungryCoder Avatar answered Oct 31 '22 14:10

HungryCoder