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 = " >>"
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
…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:
<% 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 = " >>"
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
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 %>
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
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
has examples.
Hugh
On 6/15/07, Edgar G. [email protected] 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