私はglobalize gemを使用して、Rails アプリのコンテンツを翻訳しています。言語をデフォルトの言語 :en から :de に変更するとすべて正常に動作しますが、言語を :de から :en に変更すると、NoMethodError (undefined method 'color' for nil:NilClass)
私はいくつかの調査を行い、いくつかのアプローチを試みましたが、おそらくエラーの理由であるこのビットを完全には理解していないことを認めなければなりません:
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
問題を解決する方法のヒントや、このコードがどのように機能するかについての説明をいただければ幸いです。
モデルは次のとおりです。
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
コントローラ: pages_controller.rb
def set_page
@page = Page.find_by_permalink(params[:id])
end
そしてルート:
resources :labels, do
resources :pages
end