Rails 3 route helpers in model not respecting sub-URI
Using Rails 3.2.13.
I've got Nginx and Unicorn setup to serve a Rails application from a
sub-URI. I have some views where I need to send links to resources, so I'm
using a path helper from with a model:
def to_exhibit()
return {
:label => self.id,
:name => self.name,
:edit_path =>
Rails.application.routes.url_helpers.edit_vehicle_path(self),
}
end
This will produce a URL like http://localhost:8080/vehicles/10/edit, but
what I really want is http://localhost:8080/app/vehicles/10/edit (where
/app is my sub-URI). This works fine when calling edit_vehicle_path
directly from a view. I hacked around this problem previously by creating
my own helper:
module ApplicationHelper
def self.sub_uri_path(path)
root = Rails.application.config.relative_url_root
return '%s%s' % [ root, path ]
end
end
config.relative_url_root is defined in my config/environment files. This
works, but there has to be a proper way to do it, plus I don't want to
have to maintain this when I inevitably forget about it a year from now.
No comments:
Post a Comment