# Help with this code

**URL:** <https://www.ruby-forum.com/t/help-with-this-code/106793>\
**Category:** Rails\
**Created:** [June 14, 2007, 6:35am UTC](https://www.ruby-forum.com/t/help-with-this-code/106793 "2007-06-14T06:35:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 14, 2007, 6:35am UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/1 "2007-06-14T06:35:29Z")

</div>

Hello,

I am doing a menu and i got this code, what i want is to print the  
categories and subcategories but with the products on all of these  
categories.

Actually i am printing the categories and subcategories of each one, but  
how can i print the products of each categorie?, What error i have?

These are my helpers methods:

def display\_categories(categories, parent\_id)  
ret = “

”  
for category in categories  
if category.parent\_id == parent\_id  
ret \<\< display\_category(category)  
end  
end  
ret \<\< “
”  
end

def display\_category(category)

cm = "&nbsp;&nbsp;\>\>"

```
ret = "<li>"

```

ret \<\< “\<a href=”#"\>#{category.nombre}#{cm if  
category.children.any?}"  
ret \<\< display\_products(category)  
ret \<\< display\_categories(category.children, category.id)  
ret \<\< “”  
end

def display\_products(category)  
ps = Product.find\_all\_by\_category\_id(category)  
for p in ps  
ret = “

- ”  
ret \<\< link\_to\_remote( “#{p.nombre}”,  
:url=\>{:action =\> “prod\_id”, :id =\> p.id},  
:update =\> ‘info’)  
ret \<\< “
”  
end  
end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 14, 2007, 9:12am UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/2 "2007-06-14T09:12:54Z")

</div>

…seems kinda rough;  
why not do something like this:

def display\_categories(categories, p\_id)  
list=\<\<EOF

 \<% for c in categories %\> - \<%= c if c.parent\_id==p\_id %\>
.... .... .... # AND HERE DO: 
  - \<%= c.product %\>

 \<% end %\> 
EOF list # last statement in def is the value returned end

the c.product call will bring forth the product that is associated with  
c.  
(i.e, if c.id=X then product.category\_id=X)  
the only thing u need to assure is that in your category model, the  
association is defined.

hth,

shai

Edgar G. wrote:

> Hello,
> 
> I am doing a menu and i got this code, what i want is to print the  
> categories and subcategories but with the products on all of these  
> categories.
> 
> Actually i am printing the categories and subcategories of each one, but  
> how can i print the products of each categorie?, What error i have?
> 
> These are my helpers methods:
> 
> def display\_categories(categories, parent\_id)  
> ret = “
> 
> ”  
> for category in categories  
> if category.parent\_id == parent\_id  
> ret \<\< display\_category(category)  
> end  
> end  
> ret \<\< “
> ”  
> end
> 
> def display\_category(category)
> 
> cm = "&nbsp;&nbsp;\>\>"
> 
> ```
> ret = "<li>"
> 
> ```
> 
> ret \<\< “\<a href=”#"\>#{category.nombre}#{cm if  
> category.children.any?}"  
> ret \<\< display\_products(category)  
> ret \<\< display\_categories(category.children, category.id)  
> ret \<\< “”  
> end
> 
> def display\_products(category)  
> ps = Product.find\_all\_by\_category\_id(category)  
> for p in ps  
> ret = “
> 
> - ”  
> ret \<\< link\_to\_remote( “#{p.nombre}”,  
> :url=\>{:action =\> “prod\_id”, :id =\> p.id},  
> :update =\> ‘info’)  
> ret \<\< “
> ”  
> end  
> end

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 14, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/3 "2007-06-14T17:15:22Z")

</div>

What is list=\<\<EOF?  
shai wrote:

> …seems kinda rough;  
> why not do something like this:
> 
> def display\_categories(categories, p\_id)  
> list=\<\<EOF
> 
> \<% for c in categories %\> - \<%= c if c.parent\_id==p\_id %\>

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 15, 2007, 6:12am UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/4 "2007-06-15T06:12:35Z")

</div>

Now i got this to display the products:

def display\_products(category)

```
category.productos.each do |p|
    ret = "<li>"
    ret << link_to_remote( "<span>#{p.nombre}</span>",
                    :url=>{:action => "prod_id", :id => p.id},
                    :update => 'info')
    ret << "</li>"
end

```

end

but it present an error:

can’t convert Array into String

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 14, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/5 "2007-06-14T17:41:03Z")

</div>

On Thu, 14 Jun 2007, Edgar G. wrote:

> What is list=\<\<EOF?

Assignment from ‘here document’ (document directly embedded in the  
source)

[http://ruby.about.com/od/learnruby/p/here\_document.htm](http://ruby.about.com/od/learnruby/p/here_document.htm)

has examples.

```
    Hugh

```

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [June 15, 2007, 6:23am UTC](https://www.ruby-forum.com/t/help-with-this-code/106793/6 "2007-06-15T06:23:30Z")

</div>

On 6/15/07, Edgar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> :update => 'info')
> ret << "</li>"
> end
> 
> ```
> 
> end
> 
> but it present an error:
> 
> can’t convert Array into String

this is because your display\_products returns category.productos which  
is an  
Array (sort of)

This is the last statement in your method. The each loop is basically  
spinning it’s wheels.

ret = “

- ” # Makes a new string at the beginning of each loop  
…  
ret \<\< “
”

At the end of the block you have a string. But you don’t do anything  
with  
it.

Perhaps if you collected the results of each loop in an array and then  
joined them. eg

def display\_products(category)

```
category.productos.map do |p|
    ret = "<li>"
    ret << link_to_remote( "<span>#{p.nombre}</span>",
                    :url=>{:action => "prod_id", :id => p.id},
                    :update => 'info')
    ret << "</li>"
end.join

```

end

This would return the finished string at the end of the method.

There has to be a cleaner way though. Like just appending to the view  
buffer directly.

Hope this helps  
Daniel
