vendredi 12 février 2016

Generating dynamic ERB using ERB generator

For most of my projects, I use the same ole view for my index pages. The html is usually along the lines of:

`

<h1>Chairs</h1>

<table class="table table-striped">
  <thead>
    <tr>
      <th>Color</th>
      <th>Size</th>
      <th>Fabric</th>
      <th>Description</th>
    </tr>
  </thead>

  <tbody>
    <% @chairs.each do |chair| %>
      <tr>
        <td><%= chair.color %></td>
        <td><%= chair.size %></td>
        <td><%= chair.fabric %></td>
        <td><%= chair.description %></td>
      </tr>
    <% end %>
  </tbody>
</table>`

Fairly straight forward. The issue I'm having is that the generator uses ERB tags to create the html code. I need to use ERB tags to create ERB tags.

<h1><%= plural_table_name %></h1>

<table class="table table-striped">
  <thead>
    <tr>
    <%- if !options[:fields].empty? -%>
      <%- options[:fields].each do |field| -%>
      <th><%= field.capitalize %></th>
      <%- end -%>
    <%- end -%>
    </tr>
  </thead>

  <tbody>
****  <%- @plural_table_name.each do | singular_table_name | -%>
      <%- if !options[:fields].empty? -%>
        <%- options[:fields].each do |field| -%>
          <tr>
****        <td><%= singular_table_name.field %></td>
          </tr>
        <%- end -%>
      <%- end -%>
 ****  <%- end  -%>
  </tbody>
</table>

This is what i have, but it's obviously wrong. options[:fields] is an array that's working correctly because the <thead> block is showing what I need it to. My issue is creating an ERB tag that will return an ERB string rather than running the code that's inside. I believe I isolated the issue and noted the lines with ****. I'm not sure what the proper syntax is or if it's even possible. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire