Skip to content

Instantly share code, notes, and snippets.

@xymbol
Last active December 18, 2015 13:53
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/f7f86bc4694a00724675 to your computer and use it in GitHub Desktop.
Save xymbol/f7f86bc4694a00724675 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
validates_presence_of :locale
validates_inclusion_of :locale, in: %w(en es)
def self.with_locale(locale)
where locale: locale
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 = scope
end
def show
@post = scope.find params[:id]
end
private
def scope
Post.with_locale I18n.locale
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment