Hey all, layout_helper.rb: def table(collection, header_names, fields, class_name) return false unless collection.any? table_str = "" table_str += "<table id=\"" + class_name + "\" class=\"" + class_name + "\">\n" table_str += "\t<thead>\n" table_str += "\t\t<tr>\n" header_names.each do |name| table_str += "\t\t\t<th>" table_str += name table_str += "</th>\n" end table_str += "\t\t</tr>\n" table_str += "\t</thead>\n" table_str += "\t<tbody>\n" collection.each do |col| table_str += "\t\t<tr>\n" fields.each do |name| table_str += "\t\t\t<td>\n" table_str += col[name].to_s table_str += "\t\t\t</td>\n" end table_str += "\t\t</tr>\n" end table_str += "\t</tbody>\n" table_str += "</table>\n" end users_controller.rb: def index @users = User.find(:all,:order => ''id ASC'') @headers = ["id","Email","Name","Company","Telephone"] @fields = [:id, :email,:last_name,:company,:telephone] end users/index.html.erb: <%= table(@users, @headers, @fields,''table_class'') %> What the above does is render a big string of what I am wanting to display as html markup. If I add puts(table_str) to the helper method, then nothing gets rendered on the page. Any idea what I am doing wrong? Thanks for response. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 18, 7:06 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey all, > > layout_helper.rb: > > def table(collection, header_names, fields, class_name) > return false unless collection.any? > table_str = "" > table_str += "<table id=\"" + class_name + "\" class=\"" + class_name > + "\">\n" > table_str += "\t<thead>\n" > table_str += "\t\t<tr>\n" > header_names.each do |name| > table_str += "\t\t\t<th>" > table_str += name > table_str += "</th>\n" > end > table_str += "\t\t</tr>\n" > table_str += "\t</thead>\n" > table_str += "\t<tbody>\n" > collection.each do |col| > table_str += "\t\t<tr>\n" > fields.each do |name| > table_str += "\t\t\t<td>\n" > table_str += col[name].to_s > table_str += "\t\t\t</td>\n" > end > table_str += "\t\t</tr>\n" > end > table_str += "\t</tbody>\n" > table_str += "</table>\n" > > end > > users_controller.rb: > > def index > @users = User.find(:all,:order => ''id ASC'') > @headers = ["id","Email","Name","Company","Telephone"] > @fields = [:id, :email,:last_name,:company,:telephone] > end > > users/index.html.erb: > <%= table(@users, @headers, @fields,''table_class'') %> > > What the above does is render a big string of what I am wanting to > display as html markup. If I add puts(table_str) to the helper method, > then nothing gets rendered on the page. > > Any idea what I am doing wrong?Have you checked that your table method is actually being called (eg by sticking a breakpoint in it or some calls to Rails.logger.info) ? Fred> > Thanks for response. > > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #988214:> On Mar 18, 7:06pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> table_str += "\t\t<tr>\n" >> fields.each do |name| >> >> >> What the above does is render a big string of what I am wanting to >> display as html markup. If I add puts(table_str) to the helper method, >> then nothing gets rendered on the page. >> >> Any idea what I am doing wrong? > > Have you checked that your table method is actually being called (eg > by sticking a breakpoint in it or some calls to Rails.logger.info) ? > > FredIm pretty sure it gets called because it outputs html as one huge string e.g. ''<table><th>Item</th>'' and one long string displays in the browser. That''s when I don''t add puts at the end of the helper method. When I add puts and pass in the concatenated string, nothing gets displayed at all. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 18, 7:58 pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Frederick Cheung wrote in post #988214: > > > On Mar 18, 7:06pm, John Merlino <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> table_str += "\t\t<tr>\n" > >> fields.each do |name| > > >> What the above does is render a big string of what I am wanting to > >> display as html markup. If I add puts(table_str) to the helper method, > >> then nothing gets rendered on the page. > > >> Any idea what I am doing wrong? > > > Have you checked that your table method is actually being called (eg > > by sticking a breakpoint in it or some calls to Rails.logger.info) ? > > > Fred > > Im pretty sure it gets called because it outputs html as one huge string > e.g. ''<table><th>Item</th>'' and one long string displays in the browser. > That''s when I don''t add puts at the end of the helper method. When I add > puts and pass in the concatenated string, nothing gets displayed at all.Oops, had missed the comment about the output you get. Sticking in puts suppresses all output because the result of puts is always nil. You''re seeing the html because rails automatically escape html for you these days. You can call html_safe on a string to mark it as not needing escaping (although that shifts the responsibility of checking that things are indeed safe to you ) Fred> > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> You''re seeing the html because rails automatically escape > html for you these days. You can call html_safe on a string to mark it > as not needing escaping (although that shifts the responsibility of > checking that things are indeed safe to you ) > > FredThanks it worked! -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.