Forum: Ruby erb & action_view outside rails

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
764374fb2642bd608c689b3af430ef62?d=identicon&s=25 David Lewis (djlewis)
on 2008-11-16 05:58
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-16 10:15
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.
764374fb2642bd608c689b3af430ef62?d=identicon&s=25 David Lewis (djlewis)
on 2008-11-16 17:57
Brian Candler 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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-11-16 21:37
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.
This topic is locked and can not be replied to.