login community 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:

1
2
3
4
  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:

1
2
3
4
  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:

1
2
3
4
5
6
  scope 'foo' do
    resources :bars
  end

  resources :bars
end

asked Jun 27 '12 at 01:57

chrispix's gravatar image

chrispix
306

edited Jun 27 '12 at 10:16


I was finally able to get it to work with:

1
2
3
4
5
6
  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.

answered Jul 12 '12 at 18:16

chrispix's gravatar image

chrispix
306

Try setting RAILS_RELATIVE_URL_ROOT to '/foo'.

answered Jun 27 '12 at 20:48

johns's gravatar image

johns ♦♦
340427

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

(Jul 12 '12 at 18:00) chrispix chrispix's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
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

Tags:

×25
×2
×1

Asked: Jun 27 '12 at 01:57

Seen: 1,080 times

Last updated: Jul 12 '12 at 18:16

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited