Search code examples
ruby-on-railsruby-on-rails-4globalize3

nil class when changing the language from :de to :en but not the other way round


I use the globalize gem to translate content in my Rails App. Everything works fine when I change the language from the default language :en to :de but when I want to change the language from :de to :en I get a NoMethodError (undefined method 'color' for nil:NilClass)

I did some research and tried a few approches but have to admit I don't completely understand this bit which is probably the reason for the error:

application_controller.rb

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
  request.subdomain
  request.env["HTTP_ACCEPT_LANGUAGE"]
  request.remote_ip
end

def default_url_options(options = {})
  (I18n.locale.to_sym.eql?(I18n.default_locale.to_sym) ? {} : {locale: I18n.locale})
end  

I would highly appreciate a hint how to solve the problem or some explanation how this code works would be very welcome.

Here's the model:

page.rb

class Page < ActiveRecord::Base

translates :name, :permalink

validates_uniqueness_of :permalink, :message => "This url is already taken"
validates_presence_of :permalink
validates_presence_of :name
validates_format_of   :permalink, :with => /\A[a-zA-Z0-9-_]*\z/i, :message => 'Url can only contain downcase letters from a-z and numbers from 0-9 and a dash and underscore'

before_save :only_allow_one_home_page

belongs_to :label
has_many :chapters
accepts_nested_attributes_for :chapters, :allow_destroy => true

mount_uploader :backgroundimage, BackgroundimageUploader

def chapters_for_form
 collection = chapters.where(page_id: id)
 collection.any? ? collection : chapters.build
end

def to_param
  permalink
end

end

And the Controller: pages_controller.rb

def set_page
  @page = Page.find_by_permalink(params[:id])
end

And the routes:

resources :labels, do
  resources :pages
end

Solution

  • Try changing your link_to the following:

    <%= link_to 'E', params.merge(locale: "en") %>
    

    A bit of explanation:

    # You must have `before_action :set_locale` somewhere in your controllers
    # So, this method is called before your controller code does its job
    def set_locale
      # This just sets current locale to params[:locale]
      # So when you request URL like http://example.org/controller/action?locale=de,
      # params[:locale] contains 'de'
      I18n.locale = params[:locale] || I18n.default_locale
    end
    
    # default_url_options is function which, well, adds default options
    # to every call of url_for helper method
    # It is also called internally when you build paths and urls for
    # resources, like 'labels_path' or 'pages_url'
    def default_url_options(options = {})
      # This line just says to add 'locale=...' parameter (locale: I18n.locale) to the request,
      # unless default locale is selected
      # This will preserve your locale between requests
      (I18n.locale.to_sym.eql?(I18n.default_locale.to_sym) ? {} : {locale: I18n.locale})
    end
    

    Now, returning to the error. You must provide URL params to your link_to helper. You provide only 'locale', but it should somehow determine which page you want the link to be pointing to. By adding params.merge(locale: en) we instruct it to use current parameters (so it will link to current page), only additionally add locale parameter to it.