Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing helpers in rails 3 to output html
    text
    copied!<p>I'm trying my best to build a helper that outputs a &lt;'ul> consisting of all the members of a collection. For each member of the collection I want to print out a &lt;'li> that has a title, and a div of links to CRUD the member. This is pretty similar to what Rails outputs for scaffolding for the index view.</p> <p>Here is the helper I've got:</p> <pre><code>def display_all(collection_sym) collection = collection_sym.to_s.capitalize.singularize.constantize.all name = collection_sym.to_s.downcase html = '' html &lt;&lt; "&lt;ul class=\"#{name}-list\"&gt;" for member in collection do html &lt;&lt; content_tag(:li, :id =&gt; member.title.gsub(' ', '-').downcase.strip) do concat content_tag(:h1, member.title, :class =&gt; "#{name}-title") concat link_to 'Edit', "/#{name}/#{member.id}/edit" concat "\|" concat link_to 'View', "/#{name}/#{member.id}" concat "\|" concat button_to 'Delete', "/#{name}/#{member.id}", :confirm =&gt; 'Are you sure? This cannot be undone.', :method =&gt; :delete end end html &lt;&lt; '&lt;/ul&gt;' return html end </code></pre> <p>And that output exactly what I want. First of all, if anybody thinks there's a better way to do this, please feel free to correct me, I suspect that I'm doing this in a bass ackwards way, but at the moment its the only way I know how.</p> <p>I then attempted to wrap the links in a div as follows:</p> <pre><code>def display_all(collection_sym) collection = collection_sym.to_s.capitalize.singularize.constantize.all name = collection_sym.to_s.downcase html = '' html &lt;&lt; "&lt;ul class=\"#{name}-list\"&gt;" for member in collection do html &lt;&lt; content_tag(:li, :id =&gt; member.title.gsub(' ', '-').downcase.strip) do concat content_tag(:h1, member.title, :class =&gt; "#{name}-title") concat content_tag(:div, :class =&gt; "links-bar") do concat link_to 'Edit', "/#{name}/#{member.id}/edit" concat "\|" concat link_to 'View', "/#{name}/#{member.id}" concat "\|" concat button_to 'Delete', "/#{name}/#{member.id}", :confirm =&gt; 'Are you sure? This cannot be undone.', :method =&gt; :delete end end end html &lt;&lt; '&lt;/ul&gt;' return html end </code></pre> <p>However, I now no longer get any of the markup inside the div.links-bar output to the view. I'm sure this must have something to do with block and bindings, but I can for the life of me figure out what or how to go about fixing it. Can anybody offer any help?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload