lundi 13 avril 2015

Extract column names dynamically without having to hardcode

I am building a admin page that displays all the tables from database. I want to do that without hardcoding the column names. Right now, I am hardcoding values in my view so it display values from the database table. How can I just extract the column names from the db without having to hardcode column names and just print them in table format. This way even if I have 10 tables, I can just call the table and print the column names extracting the information.


Here is the code:


Model:



class Product < ActiveRecord::Base
end


Controller:



class AdminController < ApplicationController

def index
@products = Products.all
end

end


View:



<h3 class="sub-header">Product</h3>
<table border='1' class="table table-striped" width="200">
<tr class="success">
<th>ID</th>
<th>url</th>
<th>url id</th>
<th>price id</th>
</tr>

<% @products.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.url %></td>
<td><%= user.url_id %></td>
<td><%= user.price_id %></td>
</tr>
<% end %>
</table>

Aucun commentaire:

Enregistrer un commentaire