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.
Related
Is there a gem that does this or is it baked in even? My understanding of things now is that anytime I change the path for a page (perhaps I do it for SEO) I need to create a new 301 redirect. Shouldn't this be handled automatically?
If so, how is it handled automatically and how do I make use of it?
Thanks!!!
This is a rather complicated question and depends a lot on how your routes look right now. The simple answer is: no, this is not handled automatically by Rails.
The easiest way to handle redirects like this is in the routes.rb file, redirect from the old path to the new one. Something like this:
match '/products/:id', redirect("/new_products/%{id}")
You can learn more about this kind of redirect at the Rails routing guide.
If you're changing just one instance of a product, then obviously this doesn't make a whole lot of sense -- you can't do this for every changed object. In this case, we'll assume that you aren't using standard ID-based routing; if you were then this wouldn't be a problem. (Since the ID wouldn't change, instead it seems likely your routing is based off of a field that does change -- like name or date or something.)
In this case, you probably want to extract the routing field away from whatever it currently is and into a slug column of its own. I recommend the really excellent friendly_id gem for this: it allows you to automatically generate slugs, and its history module allows you to perform lookups with an object's old slug.
Then, you can look up an object by its ID, its slug, or any of its old slugs, if you find it necessary to do so.
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.
I'm building a rails3 application and at the moment I have the following line in my routes.rb file:
get "/:id" => 'tapes#show'
In other words, you can show a Tape using website.com/tapes/1 and also by using website.com/1
(I am also using friendly_id gem so the user sees in fact a friendly URL in the form of website.com/tapename)
Now, what I would like to achieve is to do the same thing for Users pages. So instead of showing a User page using website.com/users/alex I want to be able to also use website.com/alex.
Is there a way to implement this 'users' logic in routes.rb together with the existing 'tapes' rule and somehow set a priority?
So if someone accesses website.com/alex my app would search if there is a User record with id 'alex' and if none is found then look for a Tape with id 'alex'.
Could I use some kind of Advanced Constraints in routes?
Any ideas?
Many thanks for the help,
Alex
Rails would have no way to determine which controller you were trying to access. The only way that this would be possible, is if either:
you could determine which model it would resolve to based upon some regular expression on the name.
or
You knew that user names and tape names never conflicted, and were willing to suffer the cost of hitting the database to resolve the correct controller.
This is probably a bad idea for a number of reasons, it would have performance implications, it also doesn't conform to RESTful principles, and it would probably be confusing to users.
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.
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..