Re: Display a string properly in View

Gavin
Thu, 18 Jun 2009 10:00:21 -0000

Sure,

From what you''ve written, it looks like your event model has
attributes called dreq_reqno, dreq_status and  dreq_create_time and
you''ve joined these together with the  event_detail method.

If this is the case, you could add a method in on of your helper files
(probably EventsHelper) that will build a table for you with all of
the desired columns and rows.

Here''s an example:

  def events_table(events)
concat " " # concat is equivelant to puts in this
case
    for event in events do
concat " " # you can write html tags or use the rails
view
helpers
      concat content_tag(:td, event.attribute_1) # the first columns
attribute
      concat content_tag(:td, event.attribute_2) # the second columns
attribute
concat "
    end
concat " "
    nil # important to return nil at the end of the helper method
  end

Writing html in a helper using the concat method can get a little
messy though. I would use a gem/plugin called markaby here as it''s
much cleaner.
http://railscasts.com/episodes/69-markaby-in-helper

when adding a table in your view, you''d simply use <%= events_table
(@events) %>

Thinking about it, this would probably be easier in a parial:
# parial called events
<% content_tag :table do %>
  <% for event in events do %>
    <% content_tag :tr do %>
      <%= content_tag :td, event.attribute_1 %>
              ... etc
    <% end %>
  <% end %>
<% end %>

Then call <%= render :partial => ''events'', :object =>
@events %> from
your view.

Gav