# Global Method Declaration?

**URL:** <https://www.ruby-forum.com/t/global-method-declaration/56372>\
**Category:** Rails\
**Created:** [March 30, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/global-method-declaration/56372 "2006-03-30T18:07:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 30, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/1 "2006-03-30T18:07:02Z")

</div>

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  
this:

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?

Thanks,

- Brent

---

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 30, 2006, 6:19pm UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/2 "2006-03-30T18:19:16Z")

</div>

On 3/30/06, Brent J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 30, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/3 "2006-03-30T22:17:50Z")

</div>

It does solve the problem, and is a much more elegant solution than  
dealing with it in the view! Thanks for the info.

- Brent

---

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 31, 2006, 2:43am UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/4 "2006-03-31T02:43:08Z")

</div>

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 D.

[http://blog.atomgiant.com](http://blog.atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 31, 2006, 3:34am UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/5 "2006-03-31T03:34:32Z")

</div>

dear friend,

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

what sould i do to solve this error ?

thanks

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object

---

<div class="post-metadata">

**Author:** ![Brent\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ccd318/32.png) [@Brent\_J](https://www.ruby-forum.com/u/Brent_J)\
**Post date:** [March 31, 2006, 3:01am UTC](https://www.ruby-forum.com/t/global-method-declaration/56372/6 "2006-03-31T03:01:14Z")

</div>

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
