Forum: Ruby on Rails passing an object

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.
4644878d28e339efabe43c37bc8503e5?d=identicon&s=25 Randy Brooks (rmbrooks)
on 2007-02-09 15:47
I dreated an app called employee using scaffolding. Changed the list
view to incorporate a helper to display date as follows:



<h1>Listing employees</h1>

<table>
  <tr>
  <% for column in Employee.list_columns %>
      <th class = "even"><%= column.human_name %></th>
  <% end %>
  </tr>

<% for employee in @employees %>
  <tr class = "<%= cycle("odd","even") %>">
  <% for column in Employee.list_columns %>
<!--    <td><%=h employee.send(column.name) %></td> CHANGED TO LINE
BELOW -->
        <td><%=h format_date(column.name,employee) %></td>
  <% end %>
    <td><%= link_to 'Show', :action => 'show', :id => employee %></td>
    <td><%= link_to 'Edit', :action => 'edit', :id => employee %></td>
    <td><%= link_to 'Destroy', { :action => 'destroy', :id => employee
}, :confirm => 'Are you sure?', :method =>$
  </tr>
<% end %>
</table>

<%= link_to 'Previous page', { :page => @employee_pages.current.previous
} if @employee_pages.current.previous %>
<%= link_to 'Next page', { :page => @employee_pages.current.next } if
@employee_pages.current.next %>

<br />

<%= link_to 'New employee', :action => 'new' %>




The helper is in application_helper.rb as follows:



# Methods added to this helper will be available to all templates in the
application.
module ApplicationHelper
        def format_date(col_name,date_object)
                if col_name.include? "date"
                        date_object.send(col_name).strftime('%m/%d/%Y')
                else
                        date_object.send(col_name)
                end
        end
end




Here's the rub, I want to use the helper in the show view (something
like the following):





<% for column in Employee.content_columns %>
<p>

<!--  <b><%= column.human_name %>:</b> <%= @employee.send(column.name)
%> -->
      <b><%= column.human_name %>:</b> <%= format_date(column.name,
@employee) %>
</p>
<% end %>

<%= link_to 'Edit', :action => 'edit', :id => @employee %> |
<%= link_to 'Back', :action => 'list' %>



But, I get this error:


You have a nil object when you didn't expect it!
The error occurred while evaluating nil.strftime


We've tried eveything that we can think of without success. What gives?

I appreciate your help and any suggestions regarding my problem or
suggestions regarding coding that you would care to make. Learning to
think the way in that Rails (and Ruby) work is certainly turning into an
experience.

Thanks again,

rB
Randy
This topic is locked and can not be replied to.