# Problems w/ partials and yield

**URL:** <https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408>\
**Category:** Rails\
**Created:** [December 26, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408 "2008-12-26T21:15:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [December 26, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/1 "2008-12-26T21:15:06Z")

</div>

Hello,  
I love the concept of using \<%=yield %\> to automatically render  
content but there are times where I find it a bit restraining.

For instance, I would like to to use the exact same partial for these 2  
methods:

list\_roles and create

If I try to use render :partial =\> ‘list\_roles’ in the create method,  
the only way I can get it to work is to create a \_list\_roles.html.erb  
template which seems like a bit of overkill.

The only difference that the create/list\_roles template is going to have  
is one extra dynamic table row.

Is there anyway to increase the flexibility of the yield method?

Thanks,  
Clem

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [December 27, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/2 "2008-12-27T22:19:16Z")

</div>

On Fri, Dec 26, 2008 at 3:15 PM, Clem R.  
\<removed\_email\_address@domain.invalid

> wrote:

> If I try to use render :partial =\> ‘list\_roles’ in the create method,

I’m confused by the juxtaposition of “I would like to use the exact same  
partial” and “The only difference is that …”  
But, plowing on ahead… my understanding of render :partial is that it  
looks in the view folder associated with the controller for the  
template.  
If #create and #list\_roles are in the same folder, you should be able to  
create a \_list\_roles.html.erb template which is referenced by the views  
for  
both the #create and #list\_roles actions (via render :partial =\>  
‘list\_roles’). The stuff that is identical between the two should go in  
the  
\_list\_roles.html.erb partial.

So, create.html.erb could look like this:  
\<%= render :partial =\> “list\_roles” %\>

and list\_roles.html.erb could look like this:  
\<%= render :partial =\> “list\_roles” %\>

# This is the extra line

I think I must be misunderstanding your question, since I feel like I  
just  
reiterated your original question.

Anyway, that’s my $.02.

–wpd

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [December 28, 2008, 1:37am UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/3 "2008-12-28T01:37:42Z")

</div>

Clem R. wrote:

> the only way I can get it to work is to create a \_list\_roles.html.erb  
> template which seems like a bit of overkill.
> 
> The only difference that the create/list\_roles template is going to have  
> is one extra dynamic table row.
> 
> Is there anyway to increase the flexibility of the yield method?
> 
> Thanks,  
> Clem

How about pulling the partial from a common directory?

For example: I have a site that uses a submenu specific to each  
controller. The layout calls “render :partial =\> ‘submenu’” which gets  
the partial specific for each controller.

OTOH, the main menu is common to the entire site, so it lives in the  
“app/views/generic” directory. The layout calls “render :partial =\>  
‘generic/menu’” so no matter what controller it is called from, the same  
partial is used.

HTH

Matt

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [December 28, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/4 "2008-12-28T18:11:34Z")

</div>

Are you familiar with content\_for ?

\<%= yield :list\_roles -%\>

\<% content\_for :list\_roles, ‘content here’ -%\>

or as a block

\<% content\_for :list\_roles do %\>  
content here…  
\<% end %\>

Cheers,  
Robby

On Fri, Dec 26, 2008 at 12:15 PM, Clem R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## If I try to use render :partial =\> ‘list\_roles’ in the create method,
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting w/Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [January 4, 2009, 3:29am UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/5 "2009-01-04T03:29:54Z")

</div>

I usually use the same partial for creating and editing, what you said  
is  
about right, I usually just do;  
_fields that are used for both creating and updating_

% if @user.new\_record? %\>  
%= submit\_tag ‘Create’ %\>  
% else %\>  
%= submit\_tag ‘Update’ %\>  
% end %\>

and then throw in whatever else you need to like back buttons or  
whatever.  
For updating the password, you can either just have the two password  
fields,  
which will update the passwords if a new one is set, or you can do  
another  
if @user.new\_record? statement in the form and have it show the fields  
for  
creating a password or updating a password.

Hope this helps.

On Tue, Dec 30, 2008 at 5:53 PM, Clem R.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [January 10, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/6 "2009-01-10T20:47:21Z")

</div>

Thanks all for getting back to me on this with your help and suggestions  
and sorry for the long reply.

I believe the most logical approach would go along the the lines of  
Tyler’s suggestion of handling the logic in the view as suggested below.

Thanks all - and I would say this is resolved.

Tyler Crocker wrote:

> I usually use the same partial for creating and editing, what you said  
> is  
> about right, I usually just do;  
> _fields that are used for both creating and updating_
> 
> % if @user.new\_record? %\>  
> %= submit\_tag ‘Create’ %\>  
> % else %\>  
> %= submit\_tag ‘Update’ %\>  
> % end %\>
> 
> and then throw in whatever else you need to like back buttons or  
> whatever.  
> For updating the password, you can either just have the two password  
> fields,  
> which will update the passwords if a new one is set, or you can do  
> another  
> if @user.new\_record? statement in the form and have it show the fields  
> for  
> creating a password or updating a password.
> 
> Hope this helps.
> 
> On Tue, Dec 30, 2008 at 5:53 PM, Clem R.  
> \<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![clemrock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c57346/32.png) [@clemrock](https://www.ruby-forum.com/u/clemrock)\
**Post date:** [December 31, 2008, 2:53am UTC](https://www.ruby-forum.com/t/problems-w-partials-and-yield/156408/7 "2008-12-31T02:53:30Z")

</div>

What I’m trying to accomplish is this. I have a simple layout called  
main.erb.html with a catch all \<%=yield %\> for content.

I keep running into the situation where so many actions’ partials are so  
similar it seems ridiculous to keep re-creating one for every action.  
I would like to do something like this:

I have these actions:

create\_users,  
update\_users  
update\_user\_password

Instead of creating 3 partials, it would be nice to use 1 partial and  
just show certain fields like this:

%=if controller.action\_name == ‘create\_users’ %\>  
Show these fields  
\<% end -%\>

%=if controller.action\_name == ‘update password’ %\>  
Show these fields  
\<% end -%\>

ect.

Robby R. wrote:

> Are you familiar with content\_for ?
> 
> \<%= yield :list\_roles -%\>
> 
> \<% content\_for :list\_roles, ‘content here’ -%\>
> 
> or as a block
> 
> \<% content\_for :list\_roles do %\>  
> content here…  
> \<% end %\>
> 
> Cheers,  
> Robby
> 
> On Fri, Dec 26, 2008 at 12:15 PM, Clem R.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## If I try to use render :partial =\> ‘list\_roles’ in the create method,
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> Robby R.  
> Chief Evangelist, Partner
> 
> PLANET ARGON, LLC  
> design // development // hosting w/Ruby on Rails
> 
> [http://www.planetargon.com/](http://www.planetargon.com/)  
> [http://www.robbyonrails.com/](http://www.robbyonrails.com/)  
> aim: planetargon
> 
> +1 503 445 2457  
> +1 877 55 ARGON [toll free]  
> +1 815 642 4068 [fax]
