# Copying controller methods to helpers

**URL:** <https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806>\
**Category:** Rails\
**Created:** [January 21, 2007, 7:41am UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806 "2007-01-21T07:41:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [January 21, 2007, 7:41am UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806/1 "2007-01-21T07:41:53Z")

</div>

I have a case where replicas of certain controller methods would be  
useful within the view. In particular, these are role-based user-access  
methods.

For example:  
can\_view?  
can\_modify?

These will be methods setup in the AccountSystem module which is  
included in the ApplicationController. These methods will be used as  
before\_filters to determine if the user has access to do a certain thing  
or not.

I would also like to provide these same methods to my views, but I don’t  
quite understand how the scope will work.

My views don’t have access to the “can\_view?” from AccountSystem. But  
if I put a can\_view? method in a helper, how would it call the can\_view?  
from AccountSystem?

Also, would there be a simple way to duplicate these in one fell-swoop  
to get all the methods copied over?

Jake

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [January 21, 2007, 10:50am UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806/2 "2007-01-21T10:50:37Z")

</div>

On 1/20/07, Jake J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would also like to provide these same methods to my views, but I don’t  
> quite understand how the scope will work.

In your ApplicationController:

helper\_method :can\_view?, :can\_modify?

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [January 21, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806/3 "2007-01-21T20:14:41Z")

</div>

On Sun, 2007-01-21 at 01:50 -0800, Chris W. wrote:

> Chris W.  
> [http://errtheblog.com](http://errtheblog.com)

Ok Chris, I like helper method, but sometimes the list grows too long.

I used to have following code in my application.rb

helper\_method  
:current\_url,:rand\_between,:url\_without\_port,:market\_open\_time\_in\_utc,:market\_close\_time\_in\_utc,:market\_current\_time\_in\_utc,:market\_local\_time\_as\_str,:redirect\_to\_login,:current\_domain,:diode,:triode,:pipe\_val

in those cases, what would the best approach? Moving them to a plugin?

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [January 22, 2007, 12:30am UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806/4 "2007-01-22T00:30:26Z")

</div>

On Jan 21, 2007, at 11:14 AM, Hemant K. wrote:

> > helper\_method :can\_view?, :can\_modify?  
> > helper\_method :current\_url,:rand\_between,:url\_without\_port,:market\_ope  
> > n\_time\_in\_utc,:market\_close\_time\_in\_utc,:market\_current\_time\_in\_utc,:m  
> > arket\_local\_time\_as\_str,:redirect\_to\_login,:current\_domain,:diode,:tri  
> > ode,:pipe\_val
> 
> in those cases, what would the best approach? Moving them to a plugin?

Hemant-

You could store all of those methods in a module name  
WhateverModule. Then just include that in yoru controller and view  
helpers and it will bve shared without all the ugly helper calls:

module WhateverModule  
def some\_methods  
end  
end

class ApplicationController \< …  
include WhateverModule  
end

module ApplicationHelper  
include WhateverModule  
end

Cheers-  
– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [January 22, 2007, 2:48am UTC](https://www.ruby-forum.com/t/copying-controller-methods-to-helpers/82806/5 "2007-01-22T02:48:38Z")

</div>

On Sun, 2007-01-21 at 15:28 -0800, Ezra Z. wrote:

> > >
> 
> class ApplicationController \< …  
> include WhateverModule  
> end
> 
> module ApplicationHelper  
> include WhateverModule  
> end

Hmm makes sense.

Thanks.
