2016-02-29 56 views
8

赦免的新手問題,但我發現很多的守衛功能塞像這樣的例子:模塊插頭的護罩?

plug :assign_welcome_message, "Hi!" when action in [:index, :show] 

但我發現不知道如何與模塊做這方面的例子插頭:

plug Guardian.Plug.EnsurePermissions, 
    handler: Mp.Api.AuthController, 
    admin: [:dashboard] when action in [:protected_action] 

無論我似乎移動when action in [:protected_action]要麼給我一個語法錯誤或未定義功能when/2。我知道我在做一些愚蠢的事情,但我看不到什麼!

幫助!


phoenix 1.1.4

回答

15

不傻!只是一些句法糖的結果。

Plugs take two arguments,第二個是選項的參數。在你的例子中,你想傳遞一個關鍵字列表作爲選項參數。

但是,syntactic sugar that lets your drop the square brackets只適用於關鍵字列表是該函數中的最後一個參數。

而不是

plug Guardian.Plug.EnsurePermissions, 
    handler: Mp.Api.AuthController, 
    admin: [:dashboard] when action in [:protected_action] 

嘗試明確的關鍵字列表語法:

plug Guardian.Plug.EnsurePermissions, 
    [handler: Mp.Api.AuthController, 
    admin: [:dashboard]] when action in [:protected_action] 
+2

OMG,我知道這是一些簡單的類似。謝謝!!! – neezer

+1

這給了我在我的路由器管道中相同的編譯錯誤,但在控制器中工作。任何想法爲什麼?使用鳳凰1.3.0 – Johannes

+0

@Johannes我有同樣的問題。你找到了解決方案嗎? –