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?
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.
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.
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.
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.
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 %>