routes.rb:
root :to => "posts#index"
post_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!
def index
@posts = current_user.posts.all
end
end
Если пользователь не вошел в систему, фильтр before поймает это и перенаправит куда-то (логин? Сообщение об ошибке?). В противном случае вызывается метод индекса и отображается представление индекса.
Это будет работать «из коробки» с устройством, если вы запускаете другую аутентификацию, вам нужно адаптировать и / или написать своих собственных помощников, например, как то так:
application.html.erb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
helper_method :user_signed_in?
private
def current_user
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end
def user_signed_in?
return 1 if current_user
end
def authenticate_user!
if !current_user
flash[:error] = 'You need to sign in before accessing this page!'
redirect_to signin_services_path
end
end
end