Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop & output content_tags within content_tag in helper

I'm trying a helper method that will output a list of items, to be called like so:

foo_list( ['item_one', link_to( 'item_two', '#' ) ... ] ) 

I have written the helper like so after reading Using helpers in rails 3 to output html:

def foo_list items     content_tag :ul do         items.collect {|item| content_tag(:li, item)}     end end 

However I just get an empty UL in that case, if I do this as a test:

def foo_list items     content_tag :ul do         content_tag(:li, 'foo')     end end 

I get the UL & LI as expected.

I've tried swapping it around a bit doing:

def foo_list items     contents = items.map {|item| content_tag(:li, item)}     content_tag( :ul, contents ) end 

In that case I get the whole list but the LI tags are html escaped (even though the strings are HTML safe). Doing content_tag(:ul, contents.join("\n").html_safe ) works but it feels wrong to me and I feel content_tag should work in block mode with a collection somehow.

like image 255
DEfusion Avatar asked Jan 12 '11 13:01

DEfusion


People also ask

What is definition of loop?

Definition of loop (Entry 1 of 3) 1a : a curving or doubling of a line so as to form a closed or partly open curve within itself through which another line can be passed or into which a hook may be hooked. b : such a fold of cord or ribbon serving as an ornament. 2a : something shaped like or suggestive of a loop.

What does it mean to be in a loop?

idiom informal. to have or not have the special knowledge or power that belongs to a particular group of people: You can tell she's in the loop. She always knows about policy decisions before the rest of us.

What is a loop person?

phrase. If someone is in the loop, they are part of a group of people who make decisions about important things, or they know about these decisions. If they are out of the loop, they do not make or know about important decisions. [mainly US, informal]

What is a loop in medical terms?

(lūp), 1. A sharp curve or complete bend in a vessel, cord, or other cylindric body, forming an oval or circular ring. See also: ansa.


1 Answers

Try this:

def foo_list items   content_tag :ul do       items.collect {|item| concat(content_tag(:li, item))}   end end 
like image 89
zetetic Avatar answered Sep 29 '22 13:09

zetetic