rake routes is very slow (30s in my computer) but I need it for routing spec.
So, is there a way to get all routes like rake routes? (or how rake routes works?)
I use Rails 3 and all I have seen is for Rails 3, and I found nothing I can use in the rails doc.
Rails.application.routes.routes.to_a
.to_a is optional, it just converts it to an array.
(I found this line in railties/lib/rails/tasks/routes.rake)
I use it like :
routes[10].defaults => {:action=>"edit", :controller=>"polls"}
Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac
If you're using RSpec, you can use routing specs in your tests.
Another option is the rake shell; I love it.
Related
I'm upgrading a very old rails application step by step. At the moment I am stuck at Rails 3.1. I did all the relevant steps for upgrading. For now I don't want to use assets so I disabled it in config/application.rb.
As soon as a change the rails version in my Gemfile from 3.0.20 to 3.1.12 I get a no-route-matches error. I also changed all upgrading steps back to 3.0 to see which part causes the error but it happens only when I change the line in the Gemfile.
My routes.rb:
Wawi::Application.routes.draw do
match ":controller(/:action(/:id(.:format)))"
end
Please tell me if you need more code.
rake routes:
/:controller(/:action(/:id(.:format)))
(and a warning: circular argument reference)
Maybe another useful hint: The action is part of the Application Controller.
are you calling Object#to_i on your object inside your url helper? may be the object is nil and nil.to_i is always 0. And also note, rails primary id starts from 1.
So you should give a try with running rake routes:
ruby bundle exec rake routes
After running that command you should see your url list that are available based on your route file.
Thanks to a colleague I found the solution:
In application_controller.rb session :session_key => '...' had to be changed to Rails.application.config.session_options[:key] = '...'.
My devise route helpers methods are not recognized in my Ruby functional tests.
For instance, my application's layout contains a call to edit_admin_registration_path (since my device model is Admin) and I get the following error:
ActionView::Template::Error: undefined local variable or method `edit_admin_registration_path' for #<#<Class:0x007ff332374038>:0x007ff330d54988>
when running:
rake test:functionals TEST=test/functional/my_controller_test.rb
I think I get this problem since upgrading to Rails 3.2.15. Any idea why? (devise version is 3.2.2)
Make sure that you have this route helper by:
rake routes
Seems like similar discusion Rails Can't find route in Functional Test
How can I get the list of routes programatically in a rails 2 project. In a Rails 3 project,
Rails.application.routes.routes did what I wanted. But when I try this on a Rails 2 web site. It fails with the message No application method defined on Rails object.
you can look at the code for rake routes tasks most probably under
/gems/rails/2.3.x/lib/tasks/routes.rake for Rails 2.
ActionController::Routing::Routes.routes
look at the rake task in more detail to get a better Idea
you can try,
rake routes
this gives all the routes
UPDATE:
or on Rails 3+
bundle exec rake routes
I am a newbie in Rails and I get routing errors like this:
No route matches [GET] "/static_pages/home"
I know it's because I am missing some codes in routes.rb, how do I get these routes?
Thank you
Mori's answer is spot on. But as a beginner, you will find the following command to be your friend:
rake routes
Good luck
[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
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
or load up localhost_path/rails/info/routes in your browser.
Well in Rails 4, I use rake routes. Is it that you need?
Rails >= 6.1
I just upgraded an app to 6.1.4 and noticed that rake routes no longer works.
Instead we can run bin/rails routes
To get a complete list of the available routes in your application, visit http://localhost:3000/rails/info/routes in your browser while your server is running in the development environment. You can also execute the bin/rails routes command in your terminal to produce the same output.
Docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes