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
11
votes
1 Answers
33
votes
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