Monday, 15 July 2013

check if Rails partial is empty -



check if Rails partial is empty -

i have main page responsible html/css styling, of contents come partials. partial receives locals or params, i.e. current_user or person, , displays info if any.

is there way me check if partial rendered anything? end goal this:

<% if my_partial can render %> <div class="css_for_something"> <%= render(partial: 'my_partial', locals: {...} ) %> <% else %> <div class="css_for_no_info"> <%= render else %> <% end %>

i not want partials handle styling logic; need display content if any. conversely, main page should not know logic in partial(s), such checking values or querying database.

thank you

try storing value generated render_to_string in variable:

<% partial_content = render_to_string(partial: 'my_partial', locals: {...} ).strip %>

then can see if contains content:

<% if partial_content.present? %> <%= partial_content %> <% else %> <div class="css_for_no_info"> <%= render else %> </div> <% end %>

ruby-on-rails

No comments:

Post a Comment