WebFaction
Community site: login faq

TL;DR: how can I have Rails 3 route without a path prefix, but write URLs with the prefix?

Let's say you have a rails 3 app with a controller for bars. You deploy it to your WebFaction website with a URL path of /foo. To get a list of bars, you go to http://host.com/foo/bars. The web server routes the path /foo to passenger, and when the request gets to rails' routing engine, it looks like a request for /bars.

Now you want to link to your list of bars, so you write link_to bar_path. For the link to be routed on this host, it needs to be written out as /foo/bars. In fact, links to any controller in this app must be prefixed with "/foo".

In Rails 3, this is achievable using a scope:

MyApp::Application.routes.draw do
  scope 'foo' do
    resources :bars
  end
end

Now link_to bar_path will write /foo/bars. Unfortunately, that resource now lives at /foo/foo/bars, because the first /foo is consumed routing the request to Passenger. You can fix this by making the scope optional:

MyApp::Application.routes.draw do
  scope '(foo)' do
    resources :bars
  end
end

Now both /foo/foo/bars and /foo/bars will route to BarsController. Unfortunately, link_to bar_path now writes /bars, so the request will never be routed to our rails app in the first place.

Is there a way to write the link_to so that it includes the optional scope? Or is there a simpler way to handle this setup? I know I can just duplicate all the routes with and without the scope, but that isn't very DRY:

MyApp::Application.routes.draw do
  scope 'foo' do
    resources :bars
  end

  resources :bars
end

asked 27 Jun '12, 01:57

chrispix
30137
accept rate: 100%

edited 27 Jun '12, 10:16


I was finally able to get it to work with:

MyApp::Application.routes.draw do
  for prefix in [ENV['RAILS_RELATIVE_URL_ROOT'], '/'] do
    scope prefix do
      resources :bars
    end
  end
end

Basically, this creates the routes with and without the path prefix. So with this approach, both /foo/bars and /bars will route to BarsController, but bars_path writes /foo/bars, which is what I needed. Apparently with this approach the named routes are the first set created.

permanent link

answered 12 Jul '12, 18:16

chrispix
30137
accept rate: 100%

Try setting RAILS_RELATIVE_URL_ROOT to '/foo'.

permanent link

answered 27 Jun '12, 20:48

johns
5.3k312
accept rate: 23%

As far as I can tell, RAILS_RELATIVE_URL_ROOT has no direct effect on routing

(12 Jul '12, 18:00) chrispix
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×28
×2
×1

question asked: 27 Jun '12, 01:57

question was seen: 7,557 times

last updated: 12 Jul '12, 18:16

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM