Tuesday, 15 March 2011

ruby on rails - render :partial with a manipulated :collection -


Say I have a collection of @dogs, and I want to share the collection in one place and the rest in the other. . It's easy to spit all of them together:

  render: partial = & gt; 'Dogs / summaries' ,: archive = & gt; @dogs, as: = & gt; : Dogs  

But is it possible to manipulate (refine) your collection, or is it better to make those definitions in your controller and do something like this: / P> % h2 Male Dogs: Render: Partial = & gt; 'Dogs / summaries' ,: archive = & gt; @ Mall_dogs ,: as in => : Dog% H2 Female Dog: Render: Partial = & gt; 'Dogs / summaries' ,: archive = & gt; @fimi_dogs, as: = & gt; : Dog

thanks.

The archive logic is just a list. There is no such reason why this will not work:

  Render: partial = & gt; 'Dogs / Summary', as in: = & gt; : Dog ,: collection = & gt; @ Dogs.select {| Dogs | Dog.gender == "M"}  

Personally I like to make those lists in the controller. I think that looks better, and can be made very dry with nominated scope.

IE:

Model

  class dog & lt; ActiveRecord :: base named_scope: male, terms = & gt; {: Gender = & gt; "M"} name_scope: female ,: conditions = & gt; {: Gender = & gt; "F"} ... and  

controller

  class dog controller & lt; ApplicationController ... DRF index if the consultation [: user_id] @ user = user.fid (paras [: user_id]) @ Male_dogs = @ user dag. Mall @ Foula_dogs = @ User Dags. Female and @mail_dog = dog.mail @female_dogs = Dog.female end end end  

see

 % h2 male dogs = Render: partial = & gt; 'Dogs / Summary', as in: = & gt; : Dog ,: collection = & gt; @ Male_dogs% h2 Female Dog = Render: Partial = & gt; 'Dogs / Summary', as in: = & gt; : Dog ,: collection = & gt; @adriadogs  

No comments:

Post a Comment