Forum: Ruby on Rails filter function with parameter

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.
7714385c071e185815de39ac3258f814?d=identicon&s=25 mh789 (Guest)
on 2006-06-04 07:42
Hi!

I want to add a function with a static paramter ("2" in the example) to
a filter, but somehow Rails seems to be looking for another syntax.

before_filter :check_quantity(2), :only => [:show]

doesn't work. What's the right way to do this?

Thanks a lot!
D5145c421cd25af6fa577c15219add90?d=identicon&s=25 unknown (Guest)
on 2006-06-04 11:41
(Received via mailing list)
If the parameter is static, then won't the result be static too? If
so, you could just put in the result.
-Nathan
7714385c071e185815de39ac3258f814?d=identicon&s=25 mh789 (Guest)
on 2006-06-04 12:35
> If the parameter is static, then won't the result be static too? If
> so, you could just put in the result.

No, as a filter has access to other variables as well.

Another example would be:

before_filter :check_authentication(:user), :only => [:list, :show]
before_filter :check_authentication(:admin), :only => [:create, :delete]
(Sure, I could go with two separate functions
(check_admin_authentication and check_user_authentication) in this
particular example.)

I'm sure this feature is commonly needed, so it should be implemented in
Rails. I just don't know how to use it.
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-06-05 18:32
(Received via mailing list)
On Jun 4, 2006, at 3:35 AM, mh789 wrote:

> (Sure, I could go with two separate functions
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


	You could just give your method a default parameter, and then when
it gets called without a param it will always be 2 but when you call
it form elsewhere you can give it whatever arg you want to.

before_filter :check_quantity, :only => [:show]


def check_quantity(quan = 2)
   # unless you call with an arg, quan will always be 2.
end


-Ezra
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-06-06 02:38
(Received via mailing list)
You can use blocks in a before_filter.  I'm not at home at the moment
so I have no ruby to check syntax, but something like

before_filter :only => [:show], { check_authentication(:user) }

Sorry if the syntax is not correct.  I have used this kind of filter
before to pass parameters to methods.  Note that if you need a filter
to make the user avaialbe then you need to call that filter before
this one.

eg
before_filter :set_user
before_filter :only => [:show], { check_authentication(:user) }

Hope this helps
7714385c071e185815de39ac3258f814?d=identicon&s=25 mh789 (Guest)
on 2006-06-07 10:06
Daniel ----- wrote:
> You can use blocks in a before_filter.  I'm not at home at the moment
> so I have no ruby to check syntax, but something like
>
> before_filter :only => [:show], { check_authentication(:user) }

Thanks a lot, that does the trick!

before_filter(:only => [:create, :delete]) {|controller|
controller.check_authentication('admin') }
This topic is locked and can not be replied to.