Help with this code

0 views
Skip to first unread message

Edgar Gonzalez

unread,
Jun 14, 2007, 12:35:29 AM6/14/07
to rubyonra...@googlegroups.com
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 = "<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 = "&nbsp;&nbsp;>>"

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

shai

unread,
Jun 14, 2007, 3:12:54 AM6/14/07
to rubyonra...@googlegroups.com
...seems kinda rough;
why not do something like this:

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

Edgar Gonzalez

unread,
Jun 14, 2007, 11:15:22 AM6/14/07
to rubyonra...@googlegroups.com
What is list=<<EOF?

shai wrote:
> ...seems kinda rough;
> why not do something like this:
>
> def display_categories(categories, p_id)
> list=<<EOF
> <ul>
> <% for c in categories %>
> <li><%= c if c.parent_id==p_id %></li>

--
Posted via http://www.ruby-forum.com/.

Hugh Sasse

unread,
Jun 14, 2007, 11:40:26 AM6/14/07
to rubyonra...@googlegroups.com
On Thu, 14 Jun 2007, Edgar Gonzalez 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

Edgar Gonzalez

unread,
Jun 15, 2007, 12:12:35 AM6/15/07
to rubyonra...@googlegroups.com
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

Daniel N

unread,
Jun 15, 2007, 12:22:37 AM6/15/07
to rubyonra...@googlegroups.com
On 6/15/07, Edgar Gonzalez <rails-mai...@andreas-s.net> wrote:

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

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 = "<li>"  # Makes a new string at the beginning of each loop
...
ret << "</li>"

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



Reply all
Reply to author
Forward
0 new messages