Query which controller will be used for a URL - ruby-on-rails

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.

Related

Rails: find routes/methods which no longer match any controller

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

Rails "Show" routes

I'm having trouble creating routes for my Rails app and google is returning mostly results for rails 2-3 from 5 or more years ago.
I'm trying to create a "show" route without using resources, as I need multiple show views.
If I do the following :
get "/parameters/:id" => "parameters#show"
Then if I directly type in the path, like "parameters/12" it will work, but when I run rake routes it doesn't show a path next to the route. And I can't seem to get a path to work for it.
I'm sure I'm forgetting something really obvious here, but I've been at this problem for a few hours and haven't found a solution so I would really appreciate some help.
Because you are defining a custom route, Rails doesn't know how to generate the _url and _path named routes for you automatically.
In order to get the named routes generated, you need to help Rails by specifying the as argument
get '/parameters/:id', to: 'parameters#show', as: 'parameters_show'

Does Backbone.js have something like 'rake routes' from Ruby on Rails?

Is there an easy way to see all the routes my Backbone.js application has built while it's running?
Ruby on Rails has rake routes, which shows things like this:
unicorns GET /unicorns(.:format) unicorns#index
POST /unicorns(.:format) unicorns#create
new_unicorn GET /unicorns/new(.:format) unicorns#new
edit_unicorn GET /unicorns/:id/edit(.:format) unicorns#edit
unicorn GET /unicorns/:id(.:format) unicorns#show
PUT /unicorns/:id(.:format) unicorns#update
DELETE /unicorns/:id(.:format) unicorns#destroy
Does Backbone have an equivalent?
You can always check a router prototype/instance routes property to get all statically declared routes. So if you're not generating them programmatically, you'll get the cleanest output as so:
console.log( router.routes );
If you're using this.route() to dynamically add routes, then you should check on the Backbone.History object, this will be the complete list of routes used inside your app; although the output is a little bit messiers (routes are compiled to regexp, etc).
console.log( Backbone.history.handlers );
Note that this property ain't documented, so there's no garantee it will be kept in future version of Backbone. I'd only use it for debugging purpose.

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

Rails: how do you access RESTful helpers?

I'm trying to work through this guide to Rails routing, but I got stuck in section 3.3:
Creating a RESTful route will also make available a pile of helpers within your application
and then they list some helpers like photos_url, photos_path, etc.
My questions:
Where can I find the complete list of helpers that is "made available?"
Is there a way to call the helpers in the console? I created an app, then opened up the console with script/console. I tried to call one of the helpers on the console like this:
>> entries_url
But got:
NameError: undefined local variable or method `entries_url' for #<Object:0x349a4>
from (irb):8
You have several questions in there, most of which have already been answered by people below.
The answer to one that wasn't fully addressed however, is: yes you can use the script/console to see where your routes go. Just type in app.[route_helper] and it will respond with the path. For example app.users_path will return /users/
So for your example type app.entries_url for the full URL - or app.entries_path for its relative path within the console.
rake routes at the command line should get you that list.
I think this may be what you are looking for ... http://topfunky.com/clients/peepcode/REST-cheatsheet.pdf
You can access other helpers in the console by prepending "helper."; ie. helper.progress_box (assuming #progress_box exists of course)
From memory, you can't call url/path helpers from the console for some reason.

Resources