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


i use globalize gem translate content in rails app. works fine when change language default language :en :de when want change language :de :en nomethoderror (undefined method 'color' nil:nilclass)

i did research , tried few approches have admit don't understand bit reason 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 highly appreciate hint how solve problem or explanation how code works welcome.

here's model:

page.rb  class page < activerecord::base  translates :name, :permalink  validates_uniqueness_of :permalink, :message => "this url taken" validates_presence_of :permalink validates_presence_of :name validates_format_of   :permalink, :with => /\a[a-za-z0-9-_]*\z/i, :message => 'url can contain downcase letters a-z , numbers 0-9 , dash , 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 controller: pages_controller.rb

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

and routes:

resources :labels,   resources :pages end 

try changing link_to following:

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

a bit of explanation:

# must have `before_action :set_locale` somewhere in controllers # so, method called before controller code job def set_locale   # sets current locale params[:locale]   # when request url http://example.org/controller/action?locale=de,   # params[:locale] contains 'de'   i18n.locale = params[:locale] || i18n.default_locale end  # default_url_options function which, well, adds default options # every call of url_for helper method # called internally when build paths , urls # resources, 'labels_path' or 'pages_url' def default_url_options(options = {})   # line says add 'locale=...' parameter (locale: i18n.locale) request,   # unless default locale selected   # preserve locale between requests   (i18n.locale.to_sym.eql?(i18n.default_locale.to_sym) ? {} : {locale: i18n.locale}) end 

now, returning error. must provide url params link_to helper. provide 'locale', should somehow determine page want link pointing to. adding params.merge(locale: en) instruct use current parameters (so link current page), additionally add locale parameter it.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -