Last active
December 18, 2015 14:10
-
-
Save xymbol/409f9e238ac24d16da0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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