Forum: Ruby on Rails before_filter :action_name OR :action_name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
408a4e38946d3aa2fca0450ec357d06a?d=identicon&s=25 Shuaib Zahda (shuaib85)
on 2009-03-06 00:27
(Received via mailing list)
Hi there

I am wondering if we can use before_filter in the form of :action
OR :action

what i want to do is to implement one of the actions, if the first
failed, then go to the second

when i use
before_filter :action1
before_filter :action2

each method will run them, my case is that i want to check if one of
them is true and not both

any idea?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-06 00:36
(Received via mailing list)
On Mar 5, 11:26 pm, Shuaib85 <shuaib.za...@gmail.com> wrote:
> what i want to do is to implement one of the actions, if the first
> failed, then go to the second
>
> when i use
> before_filter :action1
> before_filter :action2
>
> each method will run them, my case is that i want to check if one of
> them is true and not both
>

sounds like you just want one filter that checks these 2 things.

Fred
408a4e38946d3aa2fca0450ec357d06a?d=identicon&s=25 Shuaib Zahda (shuaib85)
on 2009-03-06 00:47
(Received via mailing list)
basically, I made my own user access level. In one case, the admin and
the client can delete their posts. so I do not want to rewrite the
methods in different name
in my code i have something like this

before_filter :login_required, :only => [:delete, :edit, ....]
before_filter :admin_required, :only => [:delete, :edit, ....]

some other methods can only be implemented by admin

so when I am logged in as client I cannot edit the post because i need
to be admin as well, so is there a way to stop the before filter if
one of them satisfied the condition

thanks

On Mar 6, 1:35 am, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-06 01:52
(Received via mailing list)
On Mar 5, 11:46 pm, Shuaib85 <shuaib.za...@gmail.com> wrote:
> so when I am logged in as client I cannot edit the post because i need
> to be admin as well, so is there a way to stop the before filter if
> one of them satisfied the condition

No - the only way the filter chain stops is if you redirect or render
(and then the action is not executed). The obvious solution would be
to make login_required pass if the user is an admin, then in the
example you gave you would only need

before_filter :login_required, :only => [:delete, :edit, ....]

Fred
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-06 09:46
(Received via mailing list)
You could just have one new filter that calls the other two as
appropriate.

2009/3/6 Frederick Cheung <frederick.cheung@gmail.com>
This topic is locked and can not be replied to.