erb & action_view outside rails

D

David Lewis

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
 
B

Brian Candler

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.
 
D

David Lewis

Brian said:
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.
 
B

Brian Candler

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top