svenfuchs / routing-filter

routing-filter wraps around the complex beast that the Rails routing system is, allowing for unseen flexibility and power in Rails URL recognition and generation.
http://www.artweb-design.de
MIT License
464 stars 84 forks source link

0.4.0.pre Does Not Do Proper Routing #50

Closed lightbe closed 7 years ago

lightbe commented 11 years ago

I have finally had a chance to use the 0.4.0.pre version of the gem for Rails 4. However I am finding differences between this version and the one I currently use in my Rails 3 applications. I'm not sure if there are differences in the routing-filter gems or if the issue is with i18n changes in Rails 4.

Here is the code I have in both applications in application_controller.rb where I set the locale and create a cookie.

before_filter :set_locale

private def set_locale I18n.locale = (params[:locale] if params[:locale].present?) || cookies[:locale] || 'en' cookies[:locale] = I18n.locale if cookies[:locale] != I18n.locale.to_s end

Here is the code I have in config/routes.rb.

filter :locale

When I run my Rails 3 applications the locale appears in the URL link and my debug info when I click all my links. When I run my Rails 4 application the only time the locale appears in the link is when I click a link to set the locale. When I click all my other links (text or icon) the translations appear correctly but there is not a locale value in the URL or in the debug info.

Here is the code for my locale line in my header.

<%= link_to_unless_current "English", locale: "en" %> <%= link_to_unless_current "Español", locale: "es" %> <%= link_to_unless_current "Français", locale: "fr" %>

Again I'm not sure if the issue is with the 0.4.0.pre gem or if I need to make additional i18n changes in my Rails 4 application. From reading the i18n documentation I'm not seeing anything additional I need to do. There is no specific documentation about 0.4.0.pre so I assume I should be able to do what I am doing with the Rails 3 version of routing-filter.

Any help would be appreciated.

lightbe commented 11 years ago

Is there another version of this gem that works with Rails 4? I still cannot get this working.

kreintjes commented 11 years ago

I have exactly the same problem with 0.4.0.pre in combination with a Rails 4 app. Both the standard locale filter as well as a custom filter do not work properly. The locale is read out from URLs (meaning params[:locale] is properly set), but unfortunately it is not included automatically anymore in URLs generated within Rails.

lightbe commented 11 years ago

A few days ago I rewrote my routing logic to use what Rails 4 provided and removed the gem. I had another issue with my custom error pages not working anymore with I18n that was more critical for me to correct. At least I was able to get the routing working without the gem. I ended up rewriting my custom error pages in HTML and putting them in the public folder. By naming them as 404.en.html, 404.fr.html, etc. the correct page is displayed depending on the locale.

I will leave the issue open for now just in case I may decide to use this gem again.

kreintjes commented 11 years ago

This issue seems to be the same issue as https://github.com/svenfuchs/routing-filter/issues/47. The monkey patch there can help you (temporarily) fix this problem. However, I think this is not ideal.

May I ask how you solved this with the solutions provided by Rails 4?

lightbe commented 11 years ago

I removed config.exceptions_app from application.rb since it appears that it has been deprecated. This is what I used for my custom error pages.

Here is the scope statement in routes.rb I'm using.

scope "(/:locale)", locale: /#{I18n.available_locales.join("|")}/ do my routes here end

This scope statement includes the locale in the routes (i.e. /en, /fr, ) but allows the locale to default to en if no locale is in the URL. For example http://mydomain.com/home and http://mydomain.com/en/home will both display the English version of the view. You will get the same results by using the following.

scope "(/:locale)" do my routes here end

The only difference I know of is that the first scope statement will display the locales when you do rake routes to list your defined routes. The second one does not. There are probably other differences that I am not aware of.

I then created separate error pages for each locale using HTML and placed them in the public folder. I did not have to add any code to lib/tasks like earlier versions required starting with Rails 2. I guess the code that was required is now included in Rails 4. As I mentioned when I get errors while using the English version of the website the error file nnn.en.html is displayed. If I get and error using the French version nnn.fr.html is displayed, etc.