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 = "<ul>"
for category in categories
if category.parent_id == parent_id
ret << display_category(category)
end
end
ret << "</ul>"
end
def display_category(category)
cm = " >>"
ret = "<li>"
ret << "<a href=\"#\"><span>#{category.nombre}#{cm if
category.children.any?}</span></a>"
ret << display_products(category)
ret << display_categories(category.children, category.id)
ret << "</li>"
end
def display_products(category)
ps = Product.find_all_by_category_id(category)
for p in ps
ret = "<li>"
ret << link_to_remote( "<span>#{p.nombre}</span>",
:url=>{:action => "prod_id", :id => p.id},
:update => 'info')
ret << "</li>"
end
end
--
Posted via http://www.ruby-forum.com/.
def display_categories(categories, p_id)
list=<<EOF
<ul>
<% for c in categories %>
<li><%= c if c.parent_id==p_id %></li>
....
....
....
# AND HERE DO:
<ul>
<li><%= c.product %></li>
</ul>
<% end %>
</ul>
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
--
Posted via http://www.ruby-forum.com/.
>
> What is list=<<EOF?
Assignment from 'here document' (document directly embedded in the source)
http://ruby.about.com/od/learnruby/p/here_document.htm
has examples.
Hugh
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
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