coding with alpaca -39 基礎權限設定
Aug 23, 2017 · 2 min read
helper 預設只有在 view 中才能使用,若想在 controller 中使用的話,要用 include 引入 helper:
include SessionHelper而想要在所有 controller 中都能使用 SessionHelper 的話,就引入在 application controller
class ApplicationController < ActionController::Base include SessionHelperend
要讓未登入的人看不到 about 頁面:
pages controller
class PagesController < ApplicationController
before_action :authenticate_user!, only: [:about] private def authenticate_user!
redirect_to root_path unless current_user
end
end
要讓已經登入成功的人不能再進到登入頁面:
sessions controller
class SessionsController < ApplicationController
before_action :require_no_authentication, only: [:new, :create] private def require_no_authentication
redirect_to root_path if current_user
endend

