Partial, Table Layout, Multiple Controllers/Models... DRY?


I am almost embarrased to ask this and I am sure it has been answered
previously but the search feature is down on ruby-forum so I am not
finding the answer.

I have several controllers (leads, orders, activities, etc…). I have
a ton of repeated html in each of the views for these controllers. For
example, my list view has the same table/tr/td and for…in type of code
and it just utilizes different objects based on the controller (@lead,
@order, @activity, etc…).

I have created partials for the header/footer and also for individual
sections of the page (search, list, etc…). However, I would like to
further reduce the duplication.

Does anyone know the best way to do this? Ideally, I would have a
partial (or something similar) that could take in an object from any
controller, a list of columns that I want added from that object, table
headings, etc… and produce the output - independent of the

To further explain:

Assume I have a Lead. Lead has first_name, last_name, assigned_to,
created_at, phone_number_home, etc…

I want first_name, last_name and created_at to show up in the table as
rows and I want “First Name”, Last Name" and “Date Created” as the table
headings. Even if my object has 50 fields, I only want a select few of
them to show up in the list and want to be able to specify the ones to

Then, I have an activity. For Activity, I have activity_type,
start_date, created_at, end_date, assigned_to, etc… I want the
headings to be “Activity Type”, “Date Created”, and “Assigned To”.

What is the best approach to take here? If I make a change to the table
then I want it to be universal - so only one change instead of many as I
have now. Make sense?

If there are examples of this already then please point me in the right
direction. Otherwise, suggestions are greatly appreciated.



mmodica at cox dot net


I didn’t give it much time, but didn’t get any quick replies to my post.
I went ahead and did something and want to know:

  1. Is this is okay or is there is a better way to do it.
  2. Will this affect performance? Meaning, would it be better to just
    leave the repeated info in the view files instead of making the helper
    call every time?

What I did:

Application Helper:

def list_view_td_value(field_value)


def list_view_td_sort_column(column_name, display_value)
list_text =

  list_text = list_text + sort_header_tag(column_name, :title => 



Then, in my views, I can use this:

Sample Lead:
<%= list_view_td_sort_column(‘id’, ‘Lead Number’) %>
<%= list_view_td_sort_column(‘lead_type’, ‘Lead Type’) %>
<%= list_view_td_sort_column(‘assigned_to’, ‘Assigned To’) %>

<%= list_view_td_value(>
<%= list_view_td_value(lead.lead_type)%>
<%= list_view_td_value(lead.assigned_to)%>

Sample Order:
<%= list_view_td_sort_column(‘order_status’, ‘Status’) %>
<%= list_view_td_sort_column(‘customer_first_name’, ‘First Name’) %>
<%= list_view_td_sort_column(‘customer_last_name’, ‘Last Name’) %>

<%= list_view_td_value(order.order_status)%>
<%= list_view_td_value(order.customer_first_name)%>
<%= list_view_td_value(order.customer_last_name)%>




mmodica at cox dot net

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs