I am dealing with a Rails project that has a huge and complex list of routes. When I run rails routes I can see the route I want but when I look at the files that define the routes I am unable to find it. Is there any method where given the name of the route from rails routes it can give me a line number showing where that route was defined?
For Rails < 5
You can use
rails routes | grep yourSearchString
For Rails >= 5
You can use option -c to search for routes related to controllers
rails routes -c users
will give you routes of UsersController
You can use -g option to do general purpose pattern matching
rails routes -g users
will give all the routes that partially or completely matches with your search
Hope it helps :)
Related
Given a list of paths:
* /costumers/1
* /home
* /do/something/action-name
How can check the corresponding controller for these without need do the real call?
Context:
I have a spreadsheet with many routes (exported from application analytic [New Relic]).
I'm try discover how routes doesn't are being called.
This check does not be does programmatically.
I'm using rails 4.2
Thank's
You would do this with rails routes which will list all routes, verb, controller, and method.
You can search for certain routes with -g. rails routes -g /costumers for example. Don't search for /costumers/1. The 1 is not part of the route, it is the ID of the Costumer to show. Something like /costumers/:id. If your version does not support -g, pipe the output to grep rake routes | grep /costumers or use your pager rake routes | less.
You can also visit http://localhost:3000/rails/info/routes
See Rails Routing from the Outside In for more.
If I enter bin/rails routes into terminal, I see a list of routes for my Ruby application.
If I visit the address http://localhost:3000/rails/info/routes I see the same routes, but with the additional column of Helper. These helper paths are useful to have a list of to hand, but I don't want to open up the webpage each time.
Is there a way I can run bin/rails routes in terminal, with the extra column to show helper paths? (articles_path, new_articles_path etc)
I'm on Rails 5.1.4.
When you enter bin/rails routes into terminal, you see the prefix column.
That's what you are looking for.
So if there is a prefix: welcome_index, there exists welcome_index_path, welcome_index_url helpers.
You can append _path or _url to prefix to get the name of a route helper.
The helpers are listed in the leftmost column of the output of bin/rails routes. Just append _path or _url.
I've installed ruby on rails 3.2.6, and when I execute
rails server
and access to 127.0.0.1:3000 it works, however when I generate a controller, for example
rails generate controller principal
and access to 127.0.0.1:3000/somecontroller, browser show following error:
Routing Error
No route matches [GET] "/principal"
Try running rake routes for more information on available routes.
What do I need to do, and can this be simply explained?
The problem is you did not specify any actions, so your controller 'principal' is empty, no views will be created with similar names, and no routes created.
You need to do:
rails generate controller principal index [show] [edit] [update] [create] [destroy]
The name after your controller name are the action names. Since you said controller 'principal' in the singular, then it might imply that you have a singular resource. If you want to have it in the plural, make sure you say 'controller principals'.
And your routes should should show:
resource :principal [ or :principals or multiple Restful routes ]
You need to edit config/routes.rb to tell the router which controller to route your request to. The rails standard is to use RESTful routes and in the example you've given this would equate to a singular Principal resource. Therefore you'd need to add:
resource :principal
to generate a set of RESTful routes for this resource. You can see the routes generated by doing:
rake routes
If you don't care about REST then you can simply add (assuming the PrincipalController has an index method):
match 'principal' => 'principal_controller#index'
Have a look at this chapter from the Rails Guides for more info on routing:
http://guides.rubyonrails.org/routing.html
You can also generate methods in the controller and routes at the same time by supplying their names as arguments to the rails generate controller command for example:
rails generate controller principal index
I want to list all defined helper path functions (that are created from routes) in my rails 3 application, if that is possible.
Thanks,
rake routes
or
bundle exec rake routes
Update
I later found that, there is an official way to see all the routes, by going to http://localhost:3000/rails/info/routes. Official docs: https://guides.rubyonrails.org/routing.html#listing-existing-routes
Though, it may be late, But I love the error page which displays all the routes. I usually try to go at /routes (or some bogus) path directly from the browser. Rails server automatically gives me a routing error page as well as all the routes and paths defined. That was very helpful :)
So, Just go to http://localhost:3000/routes
One more solution is
Rails.application.routes.routes
http://hackingoff.com/blog/generate-rails-sitemap-from-routes/
rails routes | grep <specific resource name>
displays resource specific routes, if it is a pretty long list of routes.
Trying http://0.0.0.0:3000/routes on a Rails 5 API app (i.e.: JSON-only oriented) will (as of Rails beta 3) return
{"status":404,"error":"Not Found","exception":"#>
<ActionController::RoutingError:...
However, http://0.0.0.0:3000/rails/info/routes will render a nice, simple HTML page with routes.
CLI
updated for version 6
To list all existing routes you'll want to run the command:
bundle exec rails routes
bundle exec will
Execute a command in the context of the bundle
and rails routes will
list all of your defined routes
Example
If you have your resource route declared like so:
resource :credential, only: [:new, :create, :destroy]
Then it's helpful to pipe the output so you can grep for your specific resource.
For example:
I know this is a minor issue, but why, if you use scaffolding in RoR, can you use lines like 'new_model name here_path' in link tags, but without using scaffolding, I get a NameError? For example, I have a simple address book app that uses basic CRUD operations. I am a RoR beginner but wanted to build an app without scaffolding and these kind of things don't seem to work. I compared my config/routes.rb and app/helpers/* with those in a scaffolded app and they are no different. What am I missing?
One way to check your routes and paths is to run:
rake routes
It outputs all your routes and paths.
Scaffolding sets up resource routes in the routes.rb file. The resource routes are what give you the path and url helpers. When you don't use scaffolding the routes aren't added, you must do it by hand.
Resource Routes can be added like so:
map.resources :models
where :models is the plural name of one of your models.