Forum: Ruby on Rails Too many columns for list.rhtml to display on one page

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean C. (Guest)
on 2005-12-31 00:27
I've gone once through the Agile book and am now attempting my own rails
app.  I've created a table named 'volunteers' with over 15 columns.  The
problem is the default list.rhtml that is created with the scaffold,
shows all the columns.  I only want to show 4 of them with the list
view.

Where would I start to only return a handfull of columns to the screen?
I think I do it in the view (list.rhtml - excerpted below).  Does anyone
have an example that only prints out a subset of the columns in a table?

..........
<h1>Listing volunteers</h1>

<table>
  <tr>
  <% for column in Volunteer.content_columns %>
      <th><%= column.human_name %></th>
  <% end %>
  </tr>
 ............
Philip E. (Guest)
on 2005-12-31 00:34
(Received via mailing list)
Sean C. wrote:
> ..........
> <h1>Listing volunteers</h1>
>
> <table>
>   <tr>
>   <% for column in Volunteer.content_columns %>
>       <th><%= column.human_name %></th>
>   <% end %>
>   </tr>
>  ............
>

You can edit your model and list the columns you want to display
(anywhere after the class line):

  def self.list_columns
    self.columns_hash.values_at("name","phone","address")
  end

(edit name, phone, address to be the columns you want)

Then, tweak your list.rhtml view:

   <% for column in Volunteer.list_columns %>
     <th><%= column.human_name %></th>
   <% end %>

Hope this helps.

Phil
Alex Y. (Guest)
on 2005-12-31 00:46
(Received via mailing list)
Sean C. wrote:
> ..........
> <h1>Listing volunteers</h1>
>
> <table>
>   <tr>
>   <% for column in Volunteer.content_columns %>
>       <th><%= column.human_name %></th>
>   <% end %>
>   </tr>
>  ............
>

Something like:

<% my_content_columns = %w{foo bar qux}.collect {|colname|
Volunteer.columns_hash[colname]} %>
<table>
   <tr>
   <% for column in my_content_columns %>
       <th><%= column.human_name %></th>
   <% end %>
   </tr>
   <% for volunteer in @volunteers %>
   <tr>
     <% for column in my_content_columns %>
     <td>
       <%= volunteer.send(column.name) %>
     </td>
     <% end %>
   </tr>
   <% end %>
</table>
This topic is locked and can not be replied to.