Sunday, February 13, 2011

Can I have url_for use a prefix option in Ruby on Rails?

In a project I am working on, Apache is set up to only forward requests that come in as /prefix/* to mongrel. How can I tell ruby on rails to generate all URLs with that prefix?

I have the routes set up for forward to the correct controller action by doing this:

map.connect 'sfc/:controller/:action'

but that doesn't seem to affect the way that the url writer generates the URLs.

Any ideas?

  • The RAILS_RELATIVE_URL_ROOT environment variable should do the trick, though I haven't tried it myself.

  • What about using the :path_prefix option:

    map.connect ':controller/:action', :path_prefix => 'sfc'
    
    From Mike Breen
  • You probably have another route (probably one of the default routes at the bottom of routes.rb) that URL generation is using in preference to the sfc-prefixed match. For example, if you have

    map.connect "sfc/:controller/:action"
    map.connect ":controller/:action/:id"
    

    then url_for(:controller => 'x', :action => 'y', :id => 3) will return "/x/y/3". If you change it to

    map.connect "sfc/:controller/:action"
    map.connect "sfc/:controller/:action/:id"
    

    you should get "/sfc/x/y/3".

    From Matt Burke
  • Mongrel accepts a --prefix option that will then be prepended to all generated URLs. This is the only way I know of to be able to run multiple instances of the same application on one server.

    Corban Brook : Rails 2.3+ changes broke the --prefix option in mongrel and it has not been updated. You will have to monkey patch the code yourself to get it to work properly.
    From Kyle Boon

0 comments:

Post a Comment