# Generating html

**URL:** <https://www.ruby-forum.com/t/generating-html/121994>\
**Category:** Rails\
**Created:** [December 3, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/generating-html/121994 "2007-12-03T18:43:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/generating-html/121994/1 "2007-12-03T18:43:04Z")

</div>

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:in`url\_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:in`node\_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:in`each’  
/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:in`tree\_html’  
/usr/home/dan/progg/rails/articles/app/controllers/  
category\_controller.rb:17:in `tree' /usr/local/bin/mongrel_rails:19:in`load’  
/usr/local/bin/mongrel\_rails:19

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 6:52pm UTC](https://www.ruby-forum.com/t/generating-html/121994/2 "2007-12-03T18:52:22Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/generating-html/121994/3 "2007-12-03T19:03:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/generating-html/121994/4 "2007-12-03T20:01:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/generating-html/121994/5 "2007-12-03T20:43:04Z")

</div>

> > 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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/generating-html/121994/6 "2007-12-03T20:52:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/generating-html/121994/7 "2007-12-03T20:13:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f05b48/32.png) [@danb](https://www.ruby-forum.com/u/danb)\
**Post date:** [December 3, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/generating-html/121994/8 "2007-12-03T23:15:17Z")

</div>

On Dec 3, 1:52 pm, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.
