Skip to content

Instantly share code, notes, and snippets.

@xymbol
Last active December 18, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xymbol/409f9e238ac24d16da0a to your computer and use it in GitHub Desktop.
Save xymbol/409f9e238ac24d16da0a to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
validates_presence_of :title_en, :title_es
def localized_attribute_for(name, locale = I18n.locale)
"#{name}_#{locale}"
end
private
def method_missing(name, *args)
attribute = localized_attribute_for name
send attribute, *args if respond_to?(attribute)
end
def respond_to_missing?(name)
respond_to? localized_attribute_for(name)
end
end
class ApplicationController < ActionController::Base
before_action :set_locale
protected
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = Post.find params[:id]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment