Technique for getting rid of the catch-all route in Rails - ruby-on-rails

I just took over a Rails 3.0 project that is making use of the dreaded catchall route: match '/:controller(/:action(/:id))'. I'd like to get rid of it and replace it with proper defined routes. However, there are a large number of controllers and I want to make sure to do it without breaking anything, so I need to audit the project to see which controller actions are relying on the catch-all. Is there any tool or method to check all of the controllers to see which actions hit that particular routes.rb entry?

I had the exact same problem about a month ago. There is no easy answer that I know of, especially if it is a poorly tested application. The best method I found was:
Remove the black hole route.
Run rake routes and compare against each of my controllers.
Test everything I could find having to do with that controller and add in the missing routes where needed.
Repeat.
Best of luck to you.

Related

how to know unused routes in rails

is there any way to know unused routes in rails over some period, or by any other mean, e.g. by parsing source code and finding out which routes are not called or over some defined period routes those are not been invoked.
I would like to know this because in a huge rails app when developers start adding "resources :objects", it gets loads of unnecessary routes created at run time, I am not sure whether this hits the performance, but still the curiosity.
I found a gem that seems to do this: amatsuda/traceroute. I haven't tried it myself, but judging from the description it does the right thing - checks for resourceful routes that are defined, but a correcponding action is not created.
But I'd admit one should be very careful with analysing links that are visited and that are not. If no one ever visits this link does not mean it is not going to be visited in the future.
you can use this gem. it will give you all unused routes https://github.com/sanjay-salunkhe/route_checker
There is no build-in rails way to do that. You can try to write on your own some kind of logger of visited paths.
But try something what is done already link.

Routing error rails "Couldn't find Fixture with id=list" when url = /fixtures/list

I currently have a fixture controller that was working fine,
However now it doesnt seem to work any more.
I have the following in the fixtures_controller
def list
#fixture = Fixture.all
Please see my github for the files https://github.com/jpknegtel/st_francis
If anyone could shed some light onto this it would be a great help
Update: Looking at your code, you do not at all have routes. You need routes for telling rails how to map the url from the request to the controllers and the actions within. Routes go in the config/routes.rb file.
Your problem is that you do not have a route for /fixtures/list. Rails thinks you want to show the fixture with the ID list. And does not find any record and throws an error.
You need to add some routes. I highly recommend to read the rails guide on routing. And I also recommend the use of ressourceful routing.
If you adopt the method names in your controllers this is really easy, otherwise you have to do a lot of work on your own. Read exspecially section 2. It took quite a while for me to understand RESTful routes, so do not give up - you will love them one day. If you have more questions, feel free to ask.

What's the best guide to Rails routes for the totally confused?

I've been learning Rails but routes continues to confuse the heck out of me.
The thing that makes it most confusing, I think, is that the routes you define are sensitive to where they are defined in your routes.rb file relative to other routes.
Has anyone come across a nice simple guide that sums things up well?
The first hit on Google for "Rails routes guide" is Rails Routing from the Outside In, which is quite comprehensive.
If you're okay with spending money on a dead tree reference, The Rails Way is actually worth it. The guides posted are probably your best bet this time, but if you plan on doing a lot of Rails, this book really breaks it down and makes it understandable. It's been a tremendous help for me. Good Luck.
As a side remark:
The routes at the beginning take preference over the routes later in the file.
So whenever you want to specify some kind of catchall route (like the default routes that map every action in every controller) you need to do that at the end.
One thing to note when you are learning and experimenting with rails routes is that there is a way to see what rails is actually doing with your config/routes.rb file.
$rake routes
this will return all of the routes it has setup based upon your config/routes.rb file. This has been particularly helpful for me as I have learned about how all of the route variables names are setup, such as new_model1_model2_path(#model1).
http://guides.rubyonrails.org is a fantastic reference, and I found their guide on routing to be extremely helpful!
I just used the one at rubyonrails.org. With all the links, it's a good one..

Rails Menus

I would like to know if there is a good plugin for rendering navigation links in Rails. It would be nice if it could render links to all possible GET actions on a controller directly from the routes file.
Can I ask the purpose of your question? There are only four get actions in a RESTful controller, (index, show, edit, and new). The overhead in producing a list using a special route generator on the fly is probably too much.
You could simply create a partial that can render those four actions for whatever controller you're currently viewing (think params[:controller]).
As far as for all possible Get actions: All possible get actions would encompass the show action for each item in your database. That is, again, best handled in a partial and the use of link_to.
It's hard to give a complete answer though, because your circumstances seem unique.
I hope this helps even a little.
-Chris
While this does not answer ths specific question, you can see all of your OWN routes on the command line by running
rake routes
This will give you your own personal readout of all of your routes on the site, however like Chris said above, this isn't really a specific answer, more of an FYI.
Cheers!
You should try Mmmenu: http://github.com/snitko/mmmenu
It's very flexible.

Setting Routes Inside/Near Controller Actions?

About 10 stackoverflow.com podcasts back Jeff mentioned that instead of wiring up his routes inside of the global.asax file he instead put them inside his controllers near the actions those routes would invoke.
How does one go about doing this?
Doesn't a route have to be registered before the controller it routes to is hit? Does he do it in the constructor? My mind is baffled by a chicken vs. egg issue here.
Check out this question.
I highly recommend you this approach. I'm using it with great success. :)
Basic idea - we are using attribute to setup a route for specific action.
In global.asax at app start we use reflection to initialize routes.
EDIT:
More precise link here.
EDIT2:
Not related to question but might be worth checking out (in case you haven't):
RESTful URLs from MVCContrib.

Resources