简体   繁体   中英

Using Plugs for specific actions in Phoenix

How can I use a plug to specific actions on phoenix. The scenario being want to use a plug for a certain actions, or the opposite, not want to use the plug in a certain actions

As specified in docs plugs controller docs , we can use guard clauses

plug/2 supports guards, allowing a developer to configure a plug to only run in some particular action

plug :log_message, "before show and edit" when action in [:show, :edit]
plug :log_message, "before all but index" when not action in [:index]
# or
plug :log_message, "before all but index" when action not in [:index]

The first plug will run only when action is show or edit. The second plug will always run, except for the index action.

I found this after bit of searching at this issue . Which lead me to the docs. Hope it helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM