Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale filter generating incorrect paths when app deployed to a sub-uri #14

Open
AndrewRadev opened this issue Nov 18, 2010 · 5 comments

Comments

@AndrewRadev
Copy link

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.

@Steffeng5
Copy link

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?

@AndrewRadev
Copy link
Author

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.

@Steffeng5
Copy link

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

@AndrewRadev
Copy link
Author

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 :).

@priidikvaikla
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants