2

I have a bilingual site with nice URLs for SEO. Using Ruby on Rails 2.3.10.

routes.rb fragment:

map.connect 'order-jira-hosting/:option.html',
    :controller => 'order', :action => 'index', :locale => 'en'
map.connect 'order-jira-with-greenhopper-hosting/:option.html',
    :controller => 'order', :action => 'index', :locale => 'en', :greenhopper => true
map.connect 'zamow-hosting-jira/:option.html',
    :controller => 'order', :action => 'index', :locale => 'pl'
map.connect 'zamow-hosting-jira-z-greenhopper/:option.html',
    :controller => 'order', :action => 'index', :locale => 'pl', :greenhopper => true

As you can see, :locale and :greenhopper are "hidden" in the URL.

There is a switch so that you can change the language of the current page. See my views/layouts/default.erb:

<%= link_to image_tag('icons/polish.png',  :alt => 'polski'),  { :locale => 'pl'}, :class => 'a' %>
<%= link_to image_tag('icons/english.png', :alt => 'English'), { :locale => 'en'}, :class => 'a' %>

I simply don't specify a controller and action so that I am redirected to the current controller and action with different locale. Unfortunately, :greenhopper parameter gets lost.

  1. I am at /order-jira-with-greenhopper-hosting/11.html
    (:option => 11, :locale => 'en', :greenhopper => true)
  2. Generated links for switching languages are /order-jira-hosting/11.html and /zamow-hosting-jira/11.html
    (:option => 11, :locale => 'pl' and 'en', :greenhopper => false)...
  3. ...But they should be /order-jira-with-greenhopper-hosting/11.html and /zamow-hosting-jira-z-greenhopper/11.html
    (:option => 11, :locale => 'pl' and 'en', :greenhopper => true)

How to use link_to method so that all parameters passed to controller are preserved? Thanks for your help.

Nowaker
  • 12,154
  • 4
  • 56
  • 62

1 Answers1

3

You can base the hash you send to link_to off the params hash, which, if you passed it into link_to as-is, would reload the current page. You can use Hash.merge(other_hash) to reset the :locale key for each link:

<%= link_to '<polish image />', params.merge({:locale => 'pl'}), :class => 'a' %>

Now, params does contain controller and action keys, but they're the controller and action that generated the current page, so the link should behave just like a page refresh, with only the parameters you changed via params.merge changing.

Hope this helps!

PS: params.merge doesn't change the params hash, if you're concerned about that - the result of the merge is returned as a new hash.

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56