Created
          March 16, 2017 00:21 
        
      - 
      
 - 
        
Save dtom90/56b82bf15fa0bffa2e3b81901e1cbe26 to your computer and use it in GitHub Desktop.  
    Rails Partial to display table of ActiveRecord::Relation
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <% columns = records.klass.columns.map {|col| col.name} %> | |
| <% columns = columns - blacklist if defined? blacklist %> | |
| <table class="table table-bordered table-striped"> | |
| <tr> | |
| <% columns.each do |col| %> | |
| <th><%= records.klass.human_attribute_name col %></th> | |
| <% end %> | |
| <% helpers.each do |helper| %> | |
| <th><%= records.klass.human_attribute_name helper %></th> | |
| <% end %> | |
| </tr> | |
| <% records.each do |rec| %> | |
| <tr> | |
| <% columns.each do |col| %> | |
| <td><%= rec[col] %></td> | |
| <% end %> | |
| <% helpers.each do |helper| %> | |
| <%= method(helper).call(rec) %> | |
| <% end %> | |
| </tr> | |
| <% end %> | |
| </table> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Can be rendered within a view with something like: