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
Related
I'm in the process of creating a rails API for scheduling appointments. I'm worrying about making a generic app version first, and then I'm going to make it in to an API, as I havn't done that before.
I've been generating 4-5 scaffolds (rails generate scaffold _____ title:string description: text)
THEN running rake dbmigrate.
When I go to view the file on my local host, while running my rails server I get this error: (unfortunately I can't post images yet with my rep)
No route matches [GET] "/c4cc2"
Rails.root: /Users/Jack/Desktop/Project/CareCloudAttempt2/C4CC2
Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
end_times_path GET /end_times(.:format) end_times#index
POST /end_times(.:format) end_times#create
new_end_time_path GET /end_times/new(.:format) end_times#new
edit_end_time_path GET /end_times/:id/edit(.:format) end_times#edit
end_time_path GET /end_times/:id(.:format) end_times#show
PATCH /end_times/:id(.:format) end_times#update
PUT /end_times/:id(.:format) end_times#update
DELETE /end_times/:id(.:format) end_times#destroy
start_times_path GET /start_times(.:format) start_times#index
POST /start_times(.:format) start_times#create
I've also tried entering the name of the routes after my URL
Here are my routes:
```
Rails.application.routes.draw do
resources :end_times
resources :start_times
resources :comments
resources :last_names
resources :first_names
end
```
I was wondering if maybe I needed to run rake db:migrate after each time I scaffold, over if it was another issue.
Thanks!
Migrating after couple of scaffolds is fine, no need to worry there.
What is c4cc2 supposed to be there? Rails looks for resource with that name in routes but isn't finding any. What are you trying to do with that?
This is not necessary you have run rake db:migrate after every scaffold, but you should run rake db:migrate before perform anything with rails server. If you have pending migration you may not browse your application.
But there is no problem with running rake db:migrate after every scaffold.
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] = '...'.
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.
[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
Background: i'm using InstantRails 2.0
I wanted to add a new column to an existing table using the following syntax:
ruby script/generate migration add_fieldname_to_tablename fieldname:string
So I tried
ruby script/generate migration add_invites_to_user invites:integer
ruby script/generate migration add_invites_to_users invites:integer
And to test it further
ruby script/generate migration AddInvites
ruby script/generate migration AddInvites invites:integer
All of the above give me
builder.rb:175 in 'build': Illegal route: the :controller must be specified! (ArgumentError)
Got it,
I had specified a route without indicating the controller.
ie map.connect 'users/invite/:id'
I fixed it by adding :controller => 'users'
map.connect 'users/invite/:id', :controller => 'users'
I setup the first route while the server was running and it worked fine!
An explanation of why this happens helps:
When you run script/generate Rails will instantiate your application, which involves loading your routes amongst other things. This may seem excessive but it's "for the best", as the other things loaded in the Rails initialization process such as plugins, gems and initializers could affect how the migration operates.
So yes, if you have bad routing code it will break when you try to generate anything.