Rails: call profitability in a partial template?

I sometimes see this in a partial erb template:

<%= yield :someval %> 

In other cases, there is no way out.

What is the advantage of a partial yield call?

+6
ruby ruby-on-rails
source share
1 answer

I have used it in the past if I have a partial one that can be called from different pages, which may require contextual content from the page.

The case that I had for the menu. I had my own stock menu items, but then I had yield(:menu) , so that the user loaded the administration page, I could add menu items from the page instead of writing the condition statement in the most partial one.

This is some pseudo code:

_menu.haml

 %ul %li Home %li Users %li Roles = yield(:menu) 

users.haml

 - content_for :menu do %li Add User %li Change permissions 

roles.haml

 - content_for :menu do %li Add Role 

Unlike:

 %ul %li Home %li Users %li Roles - if current_controller == 'users' %li Add User %li Change permissions - if current_controller == 'roles' %li Add Role 

Although both are functional (if it was real code), I prefer the first method. The second one can get out of hand and get pretty ugly pretty quickly. However, this is a matter of preference.

+11
source share

All Articles