Heike
2012-Jan-10 09:36 UTC
How to render subactions html into action to sent string to view later
Hi, I''ve got a bunch of campaigns I wish to render into an index-page. Two diagrams have to be in each campaign (jQuery). One of them requires a database query within a loop (the finished orders for each day of one week). Since its bad code to put a database query in the view, and I dont want to work with 4 dimensional arrays, I thought about rendering each campaign from a seperate action, with a view attached to it. But I either get format errors on Nil if I try <% @campaigns.each do |c| %> <%= render :action => "get_single_campaign", :params => {:campaign_id => c.id} %> <% end %> or with no html at all if I''m using @get_all_campaign_output = "" @campaigns.each do |c| @get_all_campaign_output += get_single_campaign(c.id) end in the controller (output is a string). I get the feeling my whole approach is wrong, since I cant find a solution and I''ve been searching for two days already. I hope some of you can tell me how to solve this. Thanks in advance, Heike -- 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.
Dave Aronson
2012-Jan-11 21:29 UTC
Re: How to render subactions html into action to sent string to view later
On Tue, Jan 10, 2012 at 04:36, Heike <heike.hannig-C5T47zMuePKELgA04lAiVw@public.gmane.org> wrote:> I''ve got a bunch of campaigns I wish to render into an index-page. > Two diagrams have to be in each campaign (jQuery). One of them > requires a database query within a loop (the finished orders for each > day of one week). > > Since its bad code to put a database query in the view, and I dont > want to work with 4 dimensional arrays,Frankly I think the multi-dimensional arrays (or rather, hashes) will be the way to go. BTW, if I grok in fullness, you need sets of results by campaign and weekday -- if only one diagram needs them (as implied above), you''ve got three dimensions (campaign, weekday, and whatever you want to organize within that by). In the controller I''d do something like: @results = {} db_rows.each do |row| @results[row.campaign_id] ||= [] @results[row.campaign_id][row.weekday] ||= [] @results[row.campaign_id][row.weekday] << row end Then in your view you can do something like: <% @results.keys.each do |campaign_id, campaign_results| %> <% campaign_results.each_index do |day_num| %> <% next if ! campaign_results[day_num] %> <!-- no results for that day --> <% campaign_results[day_num].each do |result| %> <% end %> <% end %> <% end %> There may be variations needed if your weekdays are named rather than numbered, etc.> But I either get format errors on Nil if I try > <% @campaigns.each do |c| %> > <%= render :action => "get_single_campaign", :params => {:campaign_id > => c.id} %> > <% end %>Which part is giving you errors, and what exactly is the error? The error message should tell you what line is causing problems.> or with no html at all if I''m using > @get_all_campaign_output = "" > @campaigns.each do |c| > @get_all_campaign_output += get_single_campaign(c.id) > endComposition by string concatenation is going to be slow slow slow with a decent-sized result set, even if it works. When faced with something like that, the usual alternative is to append them all onto an array, and join it when you need the result. -Dave -- Dave Aronson, President, Dave Aronson Software Engineering and Training Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote) DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!) Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me) -- 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.