routing-filter
routing-filter copied to clipboard
Locale filter generating incorrect paths when app deployed to a sub-uri
Hello, there.
I'm currently trying to use the locale filter in an application of mine. I'm using rails 3.0.0 and routing-filter 0.2.0. I'm deploying using Phusion Passenger.
The application is deployed to a sub-uri, for example:
http://example.com/foo
The problem is that every url that is generated from the standard rails helpers does not take into account that sub-uri and instead of /foo/en/articles/, generates paths like /en/foo/articles/, which promptly break, since my apache/nginx can find nothing there.
There is no problem with recognition, if I enter the url http://example.com/foo/en/articles, the routing filter recognizes it.
From what I understood while poking around, recognition is probably not a problem, since it has the full request and can find the real root of the application. However, generation works on pure option hashes and I can either tweak those, or manupulate the resulting string. In the localization filter in particular, generating seems to call prepend_segment which is defined as
url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{segment}#{$2 == '/' ? '' : $2}" }
This works just fine in general, but doesn't do the right thing for a sub-uri deployment.
Unfortunately, I'm not very knowledgeable about the rails internals, so I have no idea what I could work with in that filter. If I had access to the current request, I could probably extract the prefix from that and shave it off from the string, but I'm not sure if that's even possible.
Regards, Andrew.
I have the same problem: I want to deploy my application to a subfolder, but the routes are specified to the root folder. Is there anything i can do?
If you're having issues with locales, you should be able to use standard rails 3 routing to do the trick.
You see, at the time, I wanted to generate routes like this:
scope "(:locale)", :locale => /en|de/ do
resources :posts
resources :categories
end
This would mean that I could go to "/en/posts" or "/de/posts" depending on the language and "/posts" would dispatch to "/en/posts" by default.
The problem was that, since the ":locale" part was optional, the standard rails helpers wouldn't work properly. For example, post_path(@post) would simply break. This is why I tried to use this plugin instead. Since then, it appears that this has been resolved somehow within rails. I'm not sure which version fixes that, since I never bothered to check, but the latest stable, 3.0.7, should be okay -- you could just remove this plugin and try it out with standard routing. If you haven't seen it, take a look at the article in the rails guides: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params
If you're not using rails 3, though, I'm afraid I can't be of any help.
Thanks for your help. The last time I try this solution, i had the same problems with the helper methods. I solved my problem with editing the locale filter in that way:
def prepend_segment!(result, segment)
url = result.is_a?(Array) ? result.first : result
url.sub!(%r(^(http.?://[^/]*)?(.*))) {
t1 = $1
t2 = $2
if t2.to_s.start_with? "/portal"
t1 = t1.to_s ? t1.to_s+"/portal" : "/portal"
t2 = t2[7..t2.length-1]
end
"#{t1}/#{segment}#{t2 == '/' ? '' : t2}"
}
end
Yes, that seems like it should work just fine for your particular app. Still, I'd recommend updating rails and trying out the standard routing again, just in case. You can always revert it later if it breaks :).
I have related problem where other gems expect that Rails root_url is ending with slash. While using routing-filter root_url is not ending with slash anymore. This is caused by this block "{$2 == '/' ? '' : $2}". Quick fix would be remove the check and let the line be like this: "url.sub!(%r(^(http.?://[^/])?(.))) { "#{$1}/#{segment}#{$2}" }" but then extension filter tests would fail.