Forum: Ruby on Rails render an alternate partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7e4e4ee43e45c1ab62f3bb21d6f4f566?d=identicon&s=25 johnsonmlw (Guest)
on 2009-01-14 23:39
(Received via mailing list)
I want to render a partial from two different views (class/index;
cohort/index) in order to iterate (?) through the pupils model using
_pupil.html.erb (in the pupils/views directory)

class has many pupils

cohort has many pupils

pupil belongs to class
pupil belongs to cohort

It works well, and the _pupil.html.erb partial is rendered correctly
from both views.

* But, and this is the thing, I want the pupil partial to be slightly
different in each case i.e. when it's called from cohort/index I want
to display the pupil data in a table, but not when it's rendered from
class/index.

How do I go about having two _pupil.html.erb partials without breaking
links between views/controllers/models please?

Thanks
--
Matt
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-01-15 00:13
(Received via mailing list)
>
> It works well, and the _pupil.html.erb partial is rendered correctly
> from both views.
>
> * But, and this is the thing, I want the pupil partial to be slightly
> different in each case i.e. when it's called from cohort/index I want
> to display the pupil data in a table, but not when it's rendered from
> class/index.
>
> How do I go about having two _pupil.html.erb partials without breaking
> links between views/controllers/models please?

Why wouldn't you just move views/pupils/_pupil.html.erb into views/
class/_pupil.html.erb and views/cohort/_pupil.html.erb and change them
to do what you want?  Then update the render :partial calls from the
two calling files to look to their own version?
7e4e4ee43e45c1ab62f3bb21d6f4f566?d=identicon&s=25 johnsonmlw (Guest)
on 2009-01-15 16:15
(Received via mailing list)
That's exactly it. Thank you!

I'll do it.

What does that bit of rendering code look like. How do I keep the
@cohort.pupils bit but tell it to go to the different directory?

At the moment it looks like this and points to the pupils directory:

<div id="pupils">
  <%= render :partial => @cohort.pupils %>
</div>

I'm aware this should be obvious but I don't understand the API docs
on this.

--
Matt
7e4e4ee43e45c1ab62f3bb21d6f4f566?d=identicon&s=25 johnsonmlw (Guest)
on 2009-01-15 19:32
(Received via mailing list)
Sorted it with :collection

Thanks for the tip.
This topic is locked and can not be replied to.