Forum: Ruby on Rails problems w/ partials and yield

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.
Clem R. (Guest)
on 2008-12-26 22:15
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
Patrick D. (Guest)
on 2008-12-27 23:19
(Received via mailing list)
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" %>
<h1>This is the extra line</h1>

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

Anyway, that's my $.02.

--wpd
Matt H. (Guest)
on 2008-12-28 02:37
(Received via mailing list)
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
Robby R. (Guest)
on 2008-12-28 19:11
(Received via mailing list)
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> wrote:
> If I try to use render :partial => 'list_roles' in the create method,
> --
> Posted via 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.robbyonrails.com/
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]
Clem R. (Guest)
on 2008-12-31 03:53
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> wrote:
>> If I try to use render :partial => 'list_roles' in the create method,
>> --
>> Posted via 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.robbyonrails.com/
> aim: planetargon
>
> +1 503 445 2457
> +1 877 55 ARGON [toll free]
> +1 815 642 4068 [fax]
Tyler Crocker (Guest)
on 2009-01-04 04:29
(Received via mailing list)
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
Clem R. (Guest)
on 2009-01-10 21:47
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
This topic is locked and can not be replied to.