Erb & action_view outside rails


#1

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


#2

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.


#3

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.


#4

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:
http://railscasts.com/episodes/132-helpers-outside-views

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