Dercni
Dercni

Reputation: 1224

Mixing code HTML in a helper

The following works however it appears very clunky when I see some of the streamlined Ruby code others use.

Is this the correct way to code when mixing Ruby and HTML in a helper?

def display_children(children)
  if children.count == 0
    "<p>No child records exist</p>".html_safe
  else  
    s = ""
    s << "<table class='table table-bordered text-center'>"
    children.in_groups_of(4) do |row_children|
      s << "<tr>"
      row_children.each do |child|
        s << "<td class='col-md-3'>"

        if child
          s << link_to(child.name, child)
        else
          s << '&nbsp;'
        end

        s << "</td>"
      end
      s << "</tr>"  
    end
    s << "</table>"
    s.html_safe
  end
end

Upvotes: 1

Views: 248

Answers (1)

Steve
Steve

Reputation: 4566

The simple answer is remove all the code from the helper and put it in a partial.

Code for selecting HTML output is generally embedded right into the HTML. Helpers are used to run more complex code like querying and prepping data to be displayed to the user.

Upvotes: 1

Related Questions