iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
0
Modern Web

Rails guide / Ruby API study系列 第 15

[ Rails guide study ] Day15 Filters

首先,來聽首歌吧!
Yes
這首歌的歌詞跟旋律非常違和,但也因此讓我覺得很特別~值得一提的是其中的帥哥 Bass 手是我以前大學的學長,他們樂團的歌都頗 Jazz ,希望大家喜歡!


Filters

今天要來看的是這篇第八章的部分

Rails 中 filter 可以有 "before", "after" 跟 "around" 這三個階段,表示這個東西要使用在每個 action 前 / 後 / 還是中間

很抽象嗎?來看看例子就懂囉!

因為 Rails Guide 的例子還滿清楚的,我就直接使用他們的例子講解了(絕對不是懶得自己打)

class ApplicationController < ActionController::Base
  before_action :require_login
 
  private
  def require_login
    unless logged_in?
      flash[:error] = "You must be logged in to access this section"
      redirect_to new_login_url # halts request cycle
    end
  end
end

在上面的例子中,定義了一個 private method require_login 並把它放在 filter before_action 中,所以在執行每個動作之前,都會先確認這個使用者的狀態是不是已經登入了

但這樣是有盲點的,你不可能到了一個新的頁面就已經是登入的狀態了(除非 cookie 已經儲存了),這時候可以用 skip_before_action 這個方法來設定例外

class LoginsController < ApplicationController
  skip_before_action :require_login, only: [:new, :create]
end

around_action的使用比較複雜,在定義方法的時候一定要搭配 yield 來使用

class UsersController < ApplicationController
  around_action :hoge

  ...
  private
    def hoge
      logger.debug "執行前處理"
      yield  # 執行 action 裡面的內容
      logger.debug "執行後處理"
    end
end

Filter 其他用法

除了在 filter 後面接方法之外,還可以接 Block 或者 Class

class ApplicationController < ActionController::Base
  before_action do |controller|
    unless controller.send(:logged_in?)
      flash[:error] = "You must be logged in to access this section"
      redirect_to new_login_url
    end
  end
end

上面這邊要注意,因為 logged_in 是一個 private 方法,對於 private 方法是不能有對象的,因此要用 send 這個方法


class ApplicationController < ActionController::Base
  before_action LoginFilter
end
 
class LoginFilter
  def self.before(controller)
    unless controller.send(:logged_in?)
      controller.flash[:error] = "You must be logged in to access this section"
      controller.redirect_to controller.new_login_url
    end
  end
end

在使用 class 的時候也要注意,這個 class 裡面的方法一定要呼應 filter 的種類,在這個例子中使用 before_action,所以裡面要有before 這個類別方法

參考資料
Rails Guide

Rails4でアクションの前後にフィルタ/処理を挟み込む

本文章同步分享於 http://anthonychao.site/


上一篇
[ Rails guide study ] Day14 Flash
下一篇
[ Rails guide study ]Day 16 Request Forgery Protection in Rails
系列文
Rails guide / Ruby API study30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言