coding with alpaca -39 基礎權限設定

JerryNil
JerryNil
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
end
end

JerryNil’s Coding Diary

Coding Diary

)
JerryNil

Written by

JerryNil

JerryNil’s Coding Diary

Coding Diary

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade