Dear all I have problem in print the string in View. @event = Event.first puts @event.event_detail dreq_reqno dreq_status dreq_create_time 09P0917228 0 18 Jun 2009 16:32 09P5075748 0 18 Jun 2009 16:32 09P5075755 0 18 Jun 2009 16:33 The format is pretty good using puts But when I print this in View, the format disorder (Some spaces are trimmed.).. <td><%= @event.event_detail.event_detail.gsub("\n", "<br>") %></td> dreq_reqno dreq_status dreq_create_time 09P0917228 0 18 Jun 2009 16:32 09P5075748 0 18 Jun 2009 16:32 09P5075755 0 18 Jun 2009 16:33 How can I have the "puts @event.event_detail" in View?? I tried this, but not work. <td><%= puts @event.event_detail %></td> Many thanks Valentino -- Posted via http://www.ruby-forum.com/.
On Thu, Jun 18, 2009 at 11:12 AM, Valentino Lun < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Dear all > > I have problem in print the string in View. > > @event = Event.first > > puts @event.event_detail > dreq_reqno dreq_status dreq_create_time > 09P0917228 0 18 Jun 2009 16:32 > 09P5075748 0 18 Jun 2009 16:32 > 09P5075755 0 18 Jun 2009 16:33 > The format is pretty good using puts > > But when I print this in View, the format disorder (Some spaces are > trimmed.).. > <td><%= @event.event_detail.event_detail.gsub("\n", "<br>") %></td> > dreq_reqno dreq_status dreq_create_time > 09P0917228 0 18 Jun 2009 16:32 > 09P5075748 0 18 Jun 2009 16:32 > 09P5075755 0 18 Jun 2009 16:33 > > How can I have the "puts @event.event_detail" in View?? I tried this, > but not work. > <td><%= puts @event.event_detail %></td> > > Many thanks > Valentino >Remove the puts and just use <%= @event.event_detail... %> The ''<%='' is like puts Andrew Timberlake http://ramblingsonrails.com http://MyMvelope.com - The SIMPLE way to manage your savings --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you for your reply It is not work using this <%= @event.event_detail %> it seems that the "\n" is not working in HTML, and the multiple space are trimmed event.event_detail.inspect => "\"dreq_reqno dreq_status dreq_create_time\\n 09P0917228 0 18 Jun 2009 16:32\\n 09P5075748 0 18 Jun 2009 16:32\\n 09P5075755 0 18 Jun 2009 16:33\\n\"" Many thanks Valentino Andrew Timberlake wrote:> On Thu, Jun 18, 2009 at 11:12 AM, Valentino Lun < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> 09P5075748 0 18 Jun 2009 16:32 >> >> How can I have the "puts @event.event_detail" in View?? I tried this, >> but not work. >> <td><%= puts @event.event_detail %></td> >> >> Many thanks >> Valentino >> > > Remove the puts and just use <%= @event.event_detail... %> > The ''<%='' is like puts > > Andrew Timberlake > http://ramblingsonrails.com > > http://MyMvelope.com - The SIMPLE way to manage your savings-- Posted via http://www.ruby-forum.com/.
Hi Valentino You shouldn''t use puts, for output in the view. You probably want to split the info for each event into seperate cells so your table is well-structured. In which case I''d write a helper_method and throw most of the work into there. If not, you can stop html from collapsing white-space by either replacing each space with (non-breaking space) or set the table''s style: table { white-space: pre; } in your css. Gav http://handyrailstips.com On Jun 18, 10:12 am, Valentino Lun <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Dear all > > I have problem in print the string in View. > > @event = Event.first > > puts @event.event_detail > dreq_reqno dreq_status dreq_create_time > 09P0917228 0 18 Jun 2009 16:32 > 09P5075748 0 18 Jun 2009 16:32 > 09P5075755 0 18 Jun 2009 16:33 > The format is pretty good using puts > > But when I print this in View, the format disorder (Some spaces are > trimmed.).. > <td><%= @event.event_detail.event_detail.gsub("\n", "<br>") %></td> > dreq_reqno dreq_status dreq_create_time > 09P0917228 0 18 Jun 2009 16:32 > 09P5075748 0 18 Jun 2009 16:32 > 09P5075755 0 18 Jun 2009 16:33 > > How can I have the "puts @event.event_detail" in View?? I tried this, > but not work. > <td><%= puts @event.event_detail %></td> > > Many thanks > Valentino > -- > Posted viahttp://www.ruby-forum.com/.
Dear Gavin Thanks for your reply, it works well after amend the css file. But I don''t understand how the helper_method can use to generate a table?? Could you further elaborate on it? (Sorry that I am new in rails..) Many thanks Valentino Gavin Morrice wrote:> Hi Valentino > > You shouldn''t use puts, for output in the view. > > You probably want to split the info for each event into seperate cells > so your table is well-structured. > In which case I''d write a helper_method and throw most of the work > into there. > > If not, you can stop html from collapsing white-space by either > replacing each space with (non-breaking space) or set the > table''s style: > > table { > white-space: pre; > } > > in your css. > > Gav > > http://handyrailstips.com > > On Jun 18, 10:12�am, Valentino Lun <rails-mailing-l...@andreas-s.net>-- Posted via http://www.ruby-forum.com/.
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 "<table>" # concat is equivelant to puts in this case for event in events do concat "<tr>" # 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 "</tr> end concat "</table>" 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