No route matches “…” with {:method=>:get}

I configured Ruby on Rails to run with Apache, because I’m not too worried about speed and didn’t want to mess with proxies. I also configured the app to run in a subdirectory, using Apache’s Alias directive to point to the app’s public directory. I’ll point out this is the first time I’ve ever looked at Ruby in my life, and my first time working with any MVC framework, although I’ve looked into them a bit.

I was getting the dreaded No route matches "/subdirectory/" with {:method=>:get} error and it seemed pretty clear what the problem was. The app didn’t know it was in a subdirectory; I’d probably need to edit the routes to tell it so. It seems this is the last thing the people in Google-land were needing to do, but I eventually figured it out. I’d need to do something like this with the routes:

map.connect 'subdirectory/:controller/:action/:id'

So I took a look at routes.rb and it was using resources, not traditional routes. So what do I do with that?

It took hours of searching before I found the answer to my problem — a testament to the quality of Rails’ documentation I suppose. The answer is path_prefix

map.resources :groups, :path_prefix => 'subdirectory/'

You can also use it for the root as well.

map.root :controller => 'start', :path_prefix => 'subdirectory/'

Now I just have to fix the fact that the author of the app hard coded all sorts of stuff with the assumption that the app wouldn’t be in a directory. Grrr.

Update: Turns out it’s even easier than that. I didn’t have to change routes.rb at all.

config.action_controller.relative_url_root = '/subdirectory'

This has the added advantage of fixing things like linked stylesheets and stuff as well.

2 Replies to “No route matches “…” with {:method=>:get}”

  1. Hi Mike,
    Thanks for the tip. I’ve just moved my rails app project from my desktop to my development server running Apache and Passenger. I ran into a RailsEnv config issue because I was in development mode. I figured that out, but was getting the routing error because I had it in a subdirectory. I googled and yours was the first site that spelled it out clearly and succinctly.
    now my subdirectory Rails app works with Passenger. Thanks!

  2. Thanks Mike, your solution to change config/environment.rb worked for me as well. I originally did the same as you and prepended the subdir name but it’s good to find that there’s a less “hacky” way or doing the same thing.

Comments are closed.