# Erb & action\_view outside rails

**URL:** <https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393>\
**Category:** Ruby\
**Created:** [November 16, 2008, 5:58am UTC](https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393 "2008-11-16T05:58:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 16, 2008, 5:58am UTC](https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393/1 "2008-11-16T05:58:29Z")

</div>

I’m using erb as a templating package to generate html files – not in  
Rails.

Question – is there a way to use the action\_view package to augment the  
templates? For example – link\_to( “clickable text”, href\_path) is in  
action\_view/helpers/url\_helper.

But I cannot set up the requires to load that. I’ve tried …

require ‘erb’  
require ‘action\_view/helpers/url\_helper’

… and many other permutations. I’ve looked at code examples online,  
worked by analogy, etc. The requires all bomb. Others that look  
similar, like – require ‘active\_support/core\_ext/hash’ – seem to work  
fine.

Are there other prerequisites? Am I missing something fundamental? Is  
this documented somewhere?

Thanks. --David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 16, 2008, 10:15am UTC](https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393/2 "2008-11-16T10:15:41Z")

</div>

This question would be better asked on a Rails mailing list.

I’ve always been under the impression that the url helpers were very  
specific to controllers, i.e. didn’t make any sense outside of a  
controller action, but I could be mistaken and the real Rails experts  
will hang out on Rails lists.

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 16, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393/3 "2008-11-16T17:57:10Z")

</div>

Brian C. wrote:

> This question would be better asked on a Rails mailing list.
> 
> I’ve always been under the impression that the url helpers were very  
> specific to controllers, i.e. didn’t make any sense outside of a  
> controller action, but I could be mistaken and the real Rails experts  
> will hang out on Rails lists.

OK, makes sense, except…

Your response gave me a clue to get through the requires, namely prepend  
– require ‘action\_controller’. But then I’ve still gotta deal with all  
the controller infrastructure that gets pulled in, so I think your  
impression is correct.

Bottom line, it isn’t worth it. Either I’ll roll my own small package,  
or…

Any suggestions for a lightweight html generation package that can be  
used inside ERb templates, outside of Rails.

Thanks. --David.

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [November 16, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/erb-action-view-outside-rails/153393/4 "2008-11-16T21:37:47Z")

</div>

David Lewis wrote:

> OK, makes sense, except…
> 
> Your response gave me a clue to get through the requires, namely prepend  
> – require ‘action\_controller’. But then I’ve still gotta deal with all  
> the controller infrastructure that gets pulled in, so I think your  
> impression is correct.

If you think about it: url\_for relies very heavily on the action  
controller routing infrastructure. For example,

url\_for(:controller=\>:foo, :action=\>:new, :id=\>123)

might map to /widget/new/123 or /widget/123/new depending on what map  
lines existed in the routes.rb file (or could not be mapped at all if  
there is no matching route). Furthermore, if you don’t provide  
:controller or :action then these default to those in the current  
request, but standalone template generation doesn’t have a concept of  
“current request”.

The following may be relevant to you:

> **[\#132 Helpers Outside Views - RailsCasts](http://railscasts.com/episodes/132-helpers-outside-views)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

It warns that many URL-related helper methods may not make sense outside  
of actionpack’s view layer.
