Generating html

How do you generate html outside of a view? I’m trying to use link_to
to generate html for a link.
I’ve included ActionView, ActionView::Helpers,
ActionView::Helpers::UrlHelper,
and ERB::Util, but the system says I’m calling url_for from a nil
object.
I checked my user code and added code to make sure each object exists
(is not null) before trying to use it. I can’t figure out what I
might have missed,
and the error seems to be occurring deep inside the libraries.
So maybe something else isn’t getting included?
Here’s the stack trace in case that helps:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.url_for

RAILS_ROOT: /usr/home/dan/progg/rails/articles/config/…
Application Trace | Framework Trace | Full Trace

/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/
helpers/url_helper.rb:27:in send' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/ helpers/url_helper.rb:27:inurl_for’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_view/
helpers/url_helper.rb:75:in link_to' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 25:innode_html’
/usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb:
15:in children_html' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 14:ineach’
/usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb:
14:in children_html' /usr/home/dan/progg/rails/articles/app/helpers/category_helper.rb: 40:intree_html’
/usr/home/dan/progg/rails/articles/app/controllers/
category_controller.rb:17:in tree' /usr/local/bin/mongrel_rails:19:inload’
/usr/local/bin/mongrel_rails:19

danb wrote:

How do you generate html outside of a view? I’m trying to use link_to
to generate html for a link.

i am not 100% sure what you are trying to do…

i don’t get the details, why use this outside a view? and if necessary,
where exactly did you place the rb file containing your code?

otherwise the stack trace looks not that strange. if all objects exist
and are not nil, my guess would be, that your routing has a bug. does
link_to with the same attributes work in a view?

danb wrote:

How do you generate html outside of a view?
I’m trying to use link_to to generate html for a link.

gemblon (t.b.) wrote:

i am not 100% sure what you are trying to do…

Thorsten M. wrote:

i don’t get the details, why use this outside a view? and if necessary,
where exactly did you place the rb file containing your code?

in helpers/category:

module CategoryHelper
class CategoryHtml < Category

otherwise the stack trace looks not that strange. if all objects exist
and are not nil, my guess would be, that your routing has a bug.
does link_to with the same attributes work in a view?

Yes, I copied the code straight out of a view that was working for
a flat list.

The db contains a virtual tree structure, sort of like a file system.
Each row (node) a has a parent_id column that points to the row that
the
current row is a branch of. The parent_id of the root node/row is nil/
null.

Now I want to view the tree by recursively calling a method that
returns a piece of html for the row (branch in the tree) passed to it
as an argument. All the little pieces of html get concatenated and
pasted into a view as a single string of pure html.

But views don’t take arguments, do they? A view would work if
it could take an argument, but it doesn’t look like they can.

But views don’t take arguments, do they? A view would work if
it could take an argument, but it doesn’t look like they can.

On Dec 3, 1:13 pm, Thorsten M. [email protected]
wrote:

in erb tags? whatever you define in a controller with an @
can be used in the view

Right, but what I’m talking about here is a partial that has multiple
copies rendered together on a page, and while the html is being
generated, there’s a stack of calls to the partial that have been
entered, but not yet exited. The stack contains the rows in the db
corresponding to a chain of nodes in the tree,

So there would be multiple values of @category or whatever that
have to be remembered while rendering the page, and the number
and structure of the rows is determined at runtime. It’s a lot
harder
without a method or something that can take its own local argument.

partials can take arguments with :locals like
<%= render :partial => “my_partial”, :locals => {:category => @category}
%>

you can make that as complex as you want by handing over arrays or
hashes
and of course, the partial could render itself recursively, since it is
a kind of method anyway

But views don’t take arguments, do they? A view would work if
it could take an argument, but it doesn’t look like they can.

in erb tags? whatever you define in a controller with an @ can be used
in the view

so in the controller:
@foo = 5

and in the view:
<%= @foo %>

would output 5

and of course you can use code:

    <% @mylist.each do |list| %>
  • <%= list.name %>
  • <% end %>

woul throw out a ul with names out of the list.

On Dec 3, 1:52 pm, Thorsten M. [email protected]
wrote:

partials can take arguments with :locals like
<%= render :partial => “my_partial”,
:locals => {:category => @category} %>

That’s it! Thorsten, you’re a genius! My gosh, I was starting to
lose hope. Anyway, the code’s working now, so my faith in Rails
is restored. Thanks Thorsten, I appreciate the help.