before_action for only one action

52 views
Skip to first unread message

Martin

unread,
Jan 27, 2016, 2:13:00 PM1/27/16
to Ruby on Rails: Talk
Hi there,

If I want a before_action for only one action, like

before_action check_something_with_redirect_if_any, only: :index

Is there another way doing that? Or is this the recommend way?

If not what else could work here?

Thanks,
Martin

Karthikeyan A K

unread,
Jan 27, 2016, 2:27:01 PM1/27/16
to rubyonra...@googlegroups.com
You can put like this

before_action check_something_with_redirect_if_any, except: [all actions except index]

but its very verbose :D

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2d11149f-2cba-4809-b769-ee9604636c11%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

tamouse pontiki

unread,
Jan 27, 2016, 9:11:30 PM1/27/16
to rubyonra...@googlegroups.com


On Jan 27, 2016 1:13 PM, "Martin" <myl...@kaffanke.at> wrote:
>
> Hi there,
>
> If I want a before_action for only one action, like
>
> before_action check_something_with_redirect_if_any, only: :index
>
> Is there another way doing that? Or is this the recommend way?

If you want it for only one action,  Jay call it in the action.

>
> If not what else could work here?
>
> Thanks,
> Martin
>

Martin

unread,
Jan 28, 2016, 4:02:55 AM1/28/16
to rubyonra...@googlegroups.com
Hi tamouse,

tamouse pontiki writes:


>> Is there another way doing that? Or is this the recommend way?
>
> If you want it for only one action, Jay call it in the action.

I tried it, but I have a view, so interesting that it doesn't work,
because they have a redirect_to inside. If I put it in the action, i
get the "dont call more than one redirect_to or render" message, which I
could fix by using some if statements, but this looks ugly. I think the
before_action's will stop if the first gets a redirect_to, so the next
will not be run anymore, and this is what I want.

Martin

--
Lösungen statt Diagnosen
Reply all
Reply to author
Forward
0 new messages