Search code examples
ruby-on-railsrubyruby-on-rails-6ruby-2.6

Run a block if a condition pass


I want to run this block only a condition is satisfied

<% if condition %>
 <% progressive_render do %>
   SLOW CODE HERE
 <% end %>
<% end %>

IF condition = true

SLOW CODE should be runnned wrapped by "progressive_render"

IF condition = true

SLOW CODE shoud be runned not wrapped by progressive_render. So runned anyway.


Solution

  • You can replace the if-else condition with a guard clause (plus unless condition):

    <% SLOW CODE HERE unless condition %>
    <% progressive_render { SLOW CODE HERE } %>