Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how to see all the "path" and "url" methods added by Rails's routing? (update: using Rails console)

[update: by not using rake routes, just to understand Rails console a little more]

It seems like inside of "rails console" for Rails 3, we can use controller, but in Rails 2.2 or 2.3, we need to use @controller

And in Rails 3, we can print out all the routes added by Rails routing for a scaffold foo:

ruby-1.9.2-p0 > puts controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n") edit_foo_path edit_foo_url foo_path foo_url foos_path foos_url new_foo_path new_foo_url 

but on Rails 2.3.8, it gives a bunch of formatted_foos_path, etc, and gives nothing for Rails 2.2.2. How to make it print out for Rails 2.3.8 and 2.2.2?


Details for Rails 2.3.8:

ruby-1.8.7-p302 > puts @controller.public_methods.grep(/path|url/).grep(/foo/).sort.join("\n") formatted_edit_foo_path formatted_edit_foo_url formatted_foo_path formatted_foo_url formatted_foos_path formatted_foos_url formatted_new_foo_path formatted_new_foo_url 
like image 502
nonopolarity Avatar asked Mar 17 '11 21:03

nonopolarity


People also ask

How do I see all routes in Rails?

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.

What is the difference between path and URL in Rails?

path is relative while url is absolute.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What are the 7 CRUD routes generated by the resources Method resources songs in Rails?

In Rails, there are seven standard CRUD actions: index, show, new, create, edit, update, and destroy, which relate to specific HTTP verbs and are usually implemented using specific ActiveRecord methods.


2 Answers

Rails 3.x–6.x

Rails.application.routes.named_routes.helper_names 

Rails 2.x

helpers = Rails.application.routes.named_routes.helpers 

This will get you all the named route methods that were created. Then you can do helpers.map(&:to_s), and whatever regex you want to get your foo versions

like image 69
nzifnab Avatar answered Sep 20 '22 16:09

nzifnab


or load up localhost_path/rails/info/routes in your browser.

like image 45
Joe Jansen Avatar answered Sep 18 '22 16:09

Joe Jansen