Erwin
2011-Jan-20 14:40 UTC
[HAML + RAILS 3] cannot output the html from my view helper... just output a string
I am stuck for hours with a view helper I cannot make it running
well .
In my haml view I wrote
%h3= I18n.t(:category_list)
#category-list
= list_all_categories(@categories)
and my helper just output all the html code as a STRING ....
def list_all_categories(categories)
if categories.size > 0
ret = "<ul>\n"
categories.each { |subcategory|
ret += "<li>\n"
ret += link_to h(subcategory.name), admin_categories_path
ret += link_to
" "+h("<add>"),
admin_categories_path
ret += "\n"
if subcategory.children.size > 0
ret += list_all_categories(subcategory.children)
end
ret += "</li>\n"
}
ret += "</ul>\n"
end
end
as you noticed I have a nested function in it ... the output html is
correct but it''s a string in the view, not the raw html
what should I use then ?
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Jan-20 15:03 UTC
Re: [HAML + RAILS 3] cannot output the html from my view helper... just output a string
On 20 January 2011 14:40, Erwin <yves_dufour-ee4meeAH724@public.gmane.org> wrote:> I am stuck for hours with a view helper I cannot make it running > well . > > In my haml view I wrote > > %h3= I18n.t(:category_list) > #category-list > = list_all_categories(@categories) > > > and my helper just output all the html code as a STRING .... > > def list_all_categories(categories) > if categories.size > 0 > ret = "<ul>\n" > categories.each { |subcategory| > ret += "<li>\n" > ret += link_to h(subcategory.name), admin_categories_path > ret += link_to " "+h("<add>"), > admin_categories_path > ret += "\n" > if subcategory.children.size > 0 > ret += list_all_categories(subcategory.children) > end > ret += "</li>\n" > } > ret += "</ul>\n" > endYou need to tell rails 3 that this is html save so insert here ret.html_safe Also make sure that you have escaped anything you got out of the db and put into ret.> end > > as you noticed I have a nested function in it ... the output html is > correct but it''s a string in the view, not the raw html-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Erwin
2011-Jan-20 16:42 UTC
Re: [HAML + RAILS 3] cannot output the html from my view helper... just output a string
thanks, i''ll test it = Haml::Engine.new(list_all_categories(@categories)).render(self) in the view seems to render correctly ... On 20 jan, 16:03, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 20 January 2011 14:40, Erwin <yves_duf...-ee4meeAH724@public.gmane.org> wrote: > > > > > > > > > > > I am stuck for hours with a view helper I cannot make it running > > well . > > > In my haml view I wrote > > > %h3= I18n.t(:category_list) > > #category-list > > = list_all_categories(@categories) > > > and my helper just output all the html code as a STRING .... > > > def list_all_categories(categories) > > if categories.size > 0 > > ret = "<ul>\n" > > categories.each { |subcategory| > > ret += "<li>\n" > > ret += link_to h(subcategory.name), admin_categories_path > > ret += link_to " "+h("<add>"), > > admin_categories_path > > ret += "\n" > > if subcategory.children.size > 0 > > ret += list_all_categories(subcategory.children) > > end > > ret += "</li>\n" > > } > > ret += "</ul>\n" > > end > > You need to tell rails 3 that this is html save so insert here > ret.html_safe > > Also make sure that you have escaped anything you got out of the db > and put into ret. > > > > > > > > > end > > > as you noticed I have a nested function in it ... the output html is > > correct but it''s a string in the view, not the raw html-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.