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 = " >>"
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/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
...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 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 = "<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/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 6/15/07, Edgar Gonzalez <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 Stringthis 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---