Forum: Ruby on Rails Global Method Declaration?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-30 18:07
(Received via mailing list)
Is there a place where I can put a method that can be accessed in both
views and controllers?

Here's the situation, maybe someone can suggest a better way to go about

I created my own custom login system with email verification and user
roles (basically, I tinkered around with SaltedHashLoginGenerator
until I finally gave up and wrote my own).  One role, of course, is an
admin role.  Users with this designation can add headline articles,
new employee bios, etc.

All methods in a controller may or may not need the admin role so I
cant use a before_filter in all of them.  So I created an is_admin
method and put it in the application controller.  Works great.
Problem is, there are times when I need to do an is_admin check inside
a view.

So where can I place is_admin so both the view and controller can call
it?  Is there a better way of doing this?


- Brent
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-03-30 18:19
(Received via mailing list)
On 3/30/06, Brent Johnson <> wrote:
> All methods in a controller may or may not need the admin role so I
> cant use a before_filter in all of them.

I know this doesn't answer your original question, but did you realize
that you could do this?

before_filter :is_admin, :only => [ :edit, :delete ]

-- OR --

before_filter :is_admin, :except => [ :show, :list ]

-- James
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-30 22:17
(Received via mailing list)
It does solve the problem, and is a much more elegant solution than
dealing with it in the view!  Thanks for the info.

- Brent
22580f640e491f0920a192610d1df393?d=identicon&s=25 Tom Davies (Guest)
on 2006-03-31 02:43
(Received via mailing list)
Hi Brent,

The before filter is the way to go for this scenario.  However, if you
find that some views are shared by both members and you only want to
show admin links to admins, you will still need to access the is_admin
method in your views.  The good news is it is very simple to do.  From
within your application controller just add:

helper_method :is_admin

And it will be available to both.

-- Tom Davies
F2a8e4cc069558b818508c80052f2f5e?d=identicon&s=25 Brent Johnson (Guest)
on 2006-03-31 03:01
(Received via mailing list)
Awesome thanks.  This is definitely true, some of the views are shared
by registered, unregistered and admin users.  The difference is that
the admin user gets to see links like "edit" or "delete" that the
normal user does not.

- Brent
Ec5ea44b42812e61857a5646b4109194?d=identicon&s=25 andre hartawan (Guest)
on 2006-03-31 03:34
(Received via mailing list)
dear friend,

I have installed radRails V0.6.1
When i tried to press ctrl+backspace in editor ,the
come up "Ruby Content Assist did not complete
normaly.please see the log for moreinformation."

what sould i do to solve this error ?


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
This topic is locked and can not be replied to.