I’m trying to build a table in Ruby on Rails with the help of the content_tag method.
When I run this:
def itemSemanticDifferential(method, options = {})
if options[:surveyQuestion].any? then
@template.content_tag(:tr) do
@template.content_tag(:td, options[:surveyQuestion], :colspan => 3)
end
end
@template.content_tag(:tr) do
@template.content_tag(:th, options[:argument0])
end
end
Only the second part gets rendered:
@template.content_tag(:tr) do
@template.content_tag(:th, options[:argument0])
end
Can anyone tell me why this is?
Ruby Rails returns the last variable it used if no return is explicitly called.
( example: )
Use an Array to return all the content_tag (WARNING: this method will return an Array, not a content_tag as you expect, you’ll need to loop on it):
As asked by author of the Question, You need to loop on the result because it is an array of content_tags. Also, you need to use
.html_safeto output the content_tags as HTML (not strings).