# ActionView helpers in a behavior - I think I got it!

**URL:** <https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770>\
**Category:** Radiant CMS\
**Created:** [July 18, 2006, 1:38am UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770 "2006-07-18T01:38:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 18, 2006, 1:38am UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/1 "2006-07-18T01:38:35Z")

</div>

Here’s the relevant parts of a navigation-tab plugin I’m in the process  
of hacking together… and I have just successfully called image\_path  
from it. Hope this helps someone.

class Behavior::Base  
define\_tags do  
tag “navtab” do |tag|  
tab = NavTab.new(tag.attr[‘label’], @request)  
# …  
end  
end  
end

class NavTab  
include ActionView::Helpers::AssetTagHelper

def initialize (label, request)  
@label = label

```
@controller = ActionController::Base.new
@controller.request = request
p image_path('hi')

```

end

# …

end

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 18, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/2 "2006-07-18T13:04:22Z")

</div>

Jay L. wrote:

> ```
> @controller = ActionController::Base.new
> @controller.request = request
> 
> ```

I think you might have to be careful with certain helper method calls -  
ISTR seeing something about this on IRC. In particular some people  
seemed to think that url\_for() might give trouble.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 18, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/3 "2006-07-18T19:07:26Z")

</div>

Andrew H. wrote:

> Jay L. wrote:
> 
> > ```
> > @controller = ActionController::Base.new
> > @controller.request = request
> > 
> > ```
> 
> I think you might have to be careful with certain helper method calls -  
> ISTR seeing something about this on IRC. In particular some people  
> seemed to think that url\_for() might give trouble.

Thanks. If I run into any weird side effects I’ll start by assuming  
they’re not my fault now…

Jay

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 18, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/4 "2006-07-18T19:21:10Z")

</div>

Jay L. wrote:

> Thanks. If I run into any weird side effects I’ll start by assuming  
> they’re not my fault now…

Well, kind of; I think the “Right Answer” might be to modify the  
Behavior API so that Radiant’s existing ActionController::Base instance  
gets passed in as a parameter rather than generating a new  
instantiation. You could default the parameter to ‘nil’ and use a new  
instance of ActionController::Base if need be, for cases where your  
modified Behavior code got installed into an older Radiant version.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 18, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/5 "2006-07-18T19:53:37Z")

</div>

Andrew H. wrote:

> modified Behavior code got installed into an older Radiant version.

That’d be really nice, actually. I think that’s what I was  
(unknowingly) asking for last month, when I got the advice to create a  
dummy controller. John, do you think it might be possible to get at the  
current ActionController from a behavior?

Jay L.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 19, 2006, 4:59am UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/6 "2006-07-19T04:59:32Z")

</div>

Jay L. wrote:

> That’d be really nice, actually. I think that’s what I was  
> (unknowingly) asking for last month, when I got the advice to create a  
> dummy controller. John, do you think it might be possible to get at the  
> current ActionController from a behavior?

I’m open to the idea but am unsure that this is the best approach.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 19, 2006, 5:02am UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/7 "2006-07-19T05:02:33Z")

</div>

John W. Long wrote:

> Jay L. wrote:
> 
> > That’d be really nice, actually. I think that’s what I was  
> > (unknowingly) asking for last month, when I got the advice to create a  
> > dummy controller. John, do you think it might be possible to get at the  
> > current ActionController from a behavior?
> 
> I’m open to the idea but am unsure that this is the best approach.

What might be better, then? An awful lot of seemingly-unrelated helpers  
in Rails need a controller, and if a dummy one causes problems, I can’t  
see any other option than the real controller.

Jay

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [July 19, 2006, 5:18am UTC](https://www.ruby-forum.com/t/actionview-helpers-in-a-behavior-i-think-i-got-it/98770/8 "2006-07-19T05:18:10Z")

</div>

Jay L. wrote:

> John W. Long wrote:
> 
> > I’m open to the idea but am unsure that this is the best approach.
> 
> What might be better, then? An awful lot of seemingly-unrelated helpers  
> in Rails need a controller, and if a dummy one causes problems, I can’t  
> see any other option than the real controller.

But that’s just it. I’m not sure that behaviors should have full access  
to the controller. It seems like it might be better for them not to rely  
on that knowledge.

Sorry to be ambivalent. I will need to think about this.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)
