Sean McMains
2006-Jan-02 05:56 UTC
[Rails] Re: Getting Index When Using render :partial (Benjamin Stiglitz)
Thanks for the suggestion, Ben. This seems an excellent approach to the problem, except that I''d been under the impression that one could only call render once. If you call it multiple times as the each_with_index loop iterates, are the results just combined automatically? How does that work? Thanks! Sean ----- Sean McMains AIM: SeanMcTex http://www.mcmains.net/ruminations On Jan 1, 2006, at 11:44 PM, rails-request@lists.rubyonrails.org wrote:> Instead of using the :collection parameter to render, render your > partial from inside an each_with_index, and pass in the index as a > parameter to the partial using :locals. > > AC::Base#render: http://api.rubyonrails.com/classes/ActionController/ > Base.html#M000178 > > -Ben-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060102/6ce4897c/attachment.html
Benjamin Stiglitz
2006-Jan-02 17:20 UTC
[Rails] Re: Getting Index When Using render :partial (Benjamin Stiglitz)
> Thanks for the suggestion, Ben. This seems an excellent approach to > the problem, except that I''d been under the impression that one > could only call render once. If you call it multiple times as the > each_with_index loop iterates, are the results just combined > automatically? How does that work?Render acts a bit different if it''s called from inside a template instead of inside a controller. I just looked back and realized there are better docs for this subject: AV::Partials: http://api.rubyonrails.com/classes/ActionView/ Partials.html One provided example: <%= render :partial => "account", :locals => { :account => @buyer } %> <% for ad in @advertisements %> <%= render :partial => "ad", :locals => { :ad => ad } %> <% end %> The result of render will be placed at the given point in the view (hence the use of <%= instead of just <%). -Ben
Rick Olson
2006-Jan-02 17:25 UTC
[Rails] Re: Getting Index When Using render :partial (Benjamin Stiglitz)
> <% for ad in @advertisements %> > <%= render :partial => "ad", :locals => { :ad => ad } %> > <% end %>Why not just: <%= render :partial => ''ad'', :collection => @advertisements %> It should set each item in the collection to the object ''ad'' inside the partial. -- rick http://techno-weenie.net
Benjamin Stiglitz
2006-Jan-02 17:31 UTC
[Rails] Re: Getting Index When Using render :partial (Benjamin Stiglitz)
> Why not just: > > <%= render :partial => ''ad'', :collection => @advertisements %> > > It should set each item in the collection to the object ''ad'' inside > the partial.That was the documentation example; in this case, Sean would use each_with_index and pass the index in the :locals. (Or, as mentioned by Justin, just use the book_counter variable; I didn''t know about that one.) -Ben