On Sun, Aug 9, 2009 at 4:04 AM, jhaagmans <jaap.haagm...@gmail.com> wrote:

>
> There is no right solution by default. My way (or equivalent) is
> useful when @posts is (or can be) in fact a collection of elements and
> you want to do the same thing for every element. If you know for a
> fact that there is only one element in the @posts object, Conrad's
> solution above will suffice and maybe even be neater. You should then
> call it @post though.
>

I would recommend doing the following when working with collections
partials can be reused:

app/views/other/index.html.erb:

render :partial => "posts/preview", :collection => @posts, :as => :post

app/views/posts/_preview.html.erb:

<% div_for post do %>
 <h2><%= link_to_unless_current h(post.title), post %></h2>
 <%= truncate(post.body, :length => 300) %> <br /><br />
<% end %>

Now, you can reuse the partial within other views keeping your
code base DRY.  The for is OK when you're not repeating the
same fragment elsewhere.  Usually, you'll catch this during a
refactoring exercise.  Lastly, Ruby for loops are implemented
as follows within an ERB template:

<% for post in @posts %>

...

<% end %>

-Conrad


>
> Eric's solution is actually the same as mine, but it deduces the
> partial name from the object and repeats the partial's contents for
> each element. If that works for you (which didn't show from your first
> post) you can also use that.
>
> Hope this helps!
>
> Jaap Haagmans
> w. http://www.relywebsolutions.nl
>
> On 9 aug, 10:39, Philip Gavrilos <rails-mailing-l...@andreas-s.net>
> wrote:
> > Eric wrote:
> > > On Aug 8, 4:08 pm, Philip Gavrilos <rails-mailing-l...@andreas-s.net>
> > > wrote:
> > >> > <% end %>
> >
> > >> i did not understand my error here its because im newbie in ruby ( :P
> )
> > >> (if _preview was _post everything was fine.. i think that is because
> > >> naming conventions right?)
> >
> > > It worked with _post because Rails creates an object for the partial
> > > with the same name as the partial filename. Since you were using
> > > post.body, etc. in the partial, Rails picked up the post object and
> > > displayed everything normally. This broke when you named the partial
> > > 'preview' because you were still using post.body, etc. When you use
> > > _preview as the partial's filename you also need to change the lines
> > > in the partial itself to preview.body, etc. in order for it to work.
> >
> > > Furthermore, you can change your instance variable to @previews and
> > > do:
> >
> > > render :partial => @previews
> >
> > > and Rails will look for a posts/_preview.html.erb file, create a
> > > 'preview' variable for each element of the @previews object, which can
> > > then be used with same preview.body, etc. syntax in the _preview
> > > partial as outlined above.
> >
> > > TIMTOWTDI, natch, but I think this all is pretty close to what you're
> > > asking about.
> >
> > > -eric
> >
> > thanks Eric.
> > your article explain me tha way that partials work. simple and clean :)
> >
> > but tight now i use the solution that Jaap told me (look above) and
> > works fine.
> > do you know the difference between them ? is Jaap solution os yours more
> > "right" ?
> > --
> > 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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to