Rails: find routes/methods which no longer match any controller - ruby-on-rails

I have a massive monolithic Rails repo with a couple thousand routes.
Over time, developers have either deleted a route which maps to an existing controller or have deleted a controller which still has a route mapped to it
Is there a smart way for me to systematically find these anomalies within my rails app to clean it up?
NOTE: Please assume that these mismatches do not present a user-facing issue but is merely for maintenance purposes to trim the number of bad routes

The traceroute gem looks very nice, but just in case it doesn't work as you expect, you could do this in rails console.
Get an array of all your controllers with their actions:
ApplicationController.descendants.map {|c| [c, c.action_methods]}
Get an array of all routes:
Rails.application.routes.routes.map &:defaults
Then you could iterate over the two arrays, selecting actions that appear in one array but not in the other one.
Before starting the console set config.eager_load = true in config/environments.development.rb. Without this you won't see all of your controllers.

There is no such command that will find you:
But here is a thing you can do:
Run command:
rails routes > routes.rb
It will write all your route in an text file and they you can match the manually if you have the time.
But there is no harm if you have extra routes present in Rails application its common in most of the Rails applications.

I found this gem which aims to solve my question somewhat
https://github.com/amatsuda/traceroute

Related

Query which controller will be used for a URL

Question
I'm looking for something in the rails console or similar where I can call get_controller_for_path('/some/path/') and it will return the corresponding controller.
Background
I have a rails project with a lot of routes. I'm investigating a routing problem and want to confirm that a given URL will match a specific route.
I can use bundle exec rake routes to view the list of routes, but that still requires my human eyes to parse the hundreds (thousands?) of routes and figure out what's going on.
Here you go: Rails parse url to hash(Routes)
That says it's Rails.application.routes.recognize_path "/accounts/1"
You can also use assert_routing in your tests. If you don't have tests, now is a very good time to add some. And you can read the source of assert_routing, and assert_recognizes, to see what they do.

Dynamic Sitemap Errors

I'm trying to get dynamic_sitemaps gem to work with my site, but the readme is very technical and a bit over my head at the moment.
I'm running into errors when trying to generate the sitemap for this bit of code.
# You can have multiple sitemaps like the above – just make sure their names are different.
# Automatically link to all pages using the routes specified
# using "resources :pages" in config/routes.rb. This will also
# automatically set <lastmod> to the date and time in page.updated_at:
#
sitemap_for :offers
It's returning the below error
ArgumentError: The collection given to sitemap_for must respond to
find_each. This is for performance. Use Model.scoped to get an ActiveRecord relation that responds to find_each.
I'm looking to have the sitemap contain all my offer posts etc.
Any help is greatly appreciated!
If your model's name is Offer, try
sitemap_for Offer.all
(note: #scoped is deprecated, so #all seems to be the better option going forward)

Random method named cv_member_url()

I'm a total noob to rails and I'm trying to understand a project I'm working on. I've found a method named cv_member_url in one of the views, but I can't for the life of me figure out where it is defined... One thing I do know about rails is that it is a very flexible language so it could be some sort of gem creating this method.
Any ideas where this method may have come from? (or better yet, how I can add others)
Thank you!
Do you have a model called CvMember? If so, the method is probably a named route for that model. See here for more info:
http://guides.rubyonrails.org/routing.html#paths-and-urls
To see all your named routes, you can run
rake routes
Those are named routes which are automatically defined based on what you have in routes.rb. *_url should be used in the controller, and *_path should be used in the views. Here's some more info from the official rails guide.
Assuming you can run this in development: You should put a breakpoint on it and step inside. Most likely it is dynamically defined or maybe in plugin.

Generating an object's absolute url without html markup

Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2

How to programmatically list all controllers and actions in RoR engines?

This question is sort of an extension of what was brought up in this discussion: How to programmatically list all controllers in Rails
It seems most of the solutions for listing out an application's controllers and actions makes use of importing and parsing #{RAILS_ROOT}/app/controllers.
I've been building and making use of RoR Engines which are in #{RAILS_ROOT}/vendor/plugins/
How could these be included to list out every engine's controllers and actions?
Weird how just writing a question out can help you figure it out. I was able to get this working by simply including the engine's controllers by running:
Find.find(File.join(RAILS_ROOT, 'vendor/plugins/')) { |name|
require_dependency(name) if /_controller\.rb$/ =~ name
}
This is pretty old, and rails 3 has changed a lot of things. I've had to go through about 3 different ways of doing this as things have changed. Currently on rails 3.2.2 this has been my best solution:
Rails.application.reload_routes!
all_routes = Rails.application.routes.routes
require 'rails/application/route_inspector'
inspector = Rails::Application::RouteInspector.new
for routeRule in inspector.format(all_routes, ENV['CONTROLLER'])
# Parse routeRule to get your values
end

Resources