Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to do an early exit or "return" from a view in Rails?

Does Rails offer any way to exit early or "return" from a view that is being rendered?

For example, I have a view structured as follows:

<h1>Your records summary</h1>

<% if @current_user.has_records? %>
  <p>You don't have any records.</p>
<% else %>
  ... long block of view emission code here ...
<% end %>

In non-erb code, I'd just return from that if condition, and wouldn't need to put that long block inside an else. It would look like:

<h1>Your records summary</h1>

<% if @current_user.has_records? %>
  <p>You don't have any records.</p>
  <% return (or something) %>
<% end %>
... long block of view emission code here ...

Is there a way to do something like that in the erb?

EDIT: To be clear, I'm not asking for alternate approaches, such as have been suggested. I'm not a Rails noob. I really just want to know whether Ruby provides a mechanism for what I suggested or not.

like image 432
Grant Birchmeier Avatar asked Jun 24 '13 18:06

Grant Birchmeier


People also ask

Does Redirect_to return?

redirect_to and render do not return Keep in mind that redirect_to and render do not cause the action to stop executing. It is not like calling return in a Ruby method.

What does break return in Ruby?

Ruby Break Keyword (Exit Loop Early) The break keyword is like next , but it ends the loop & returns a value, instead of skipping just one iteration.

How do you break out of a loop in Ruby?

In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. In examples, break statement used with if statement.

How do you exit a block in Ruby?

To break out from a ruby block simply use return keyword return if value.


2 Answers

This is an old question, but I figured I'd take the time to spare future people the hour or so of their lives trying to figure out if this is doable.

I've discovered that, as far back as Rails 3.2, this is actually possible.

It requires some knowledge of ActionView internals and I do not advocate anyone do this in production code. Further, this particular implementation only works with the built-in ERB engine used by Rails which, itself, uses Erubi.

Alright, now that we're done with the warnings, on to the action!

The surprisingly simple answer is to return @output_buffer.to_s.

As long as you've closed all your HTML tags before returning, it seems to work and everything is correctly rendered.

For the curious, this output buffer is currently configured in the initializer for the Erubi ERB handler. With the rest of the work done by the underlying Erubi::Engine implementation.

I've poked around at something that provides a less brittle way to go about returning early, but I've unsuccessful so far.

like image 165
Chris Hall Avatar answered Sep 30 '22 19:09

Chris Hall


In Rails 3.2.2, return inside a template works just fine for me.

<% return unless allowed_tags %>
like image 23
Naphtali Visser Avatar answered Sep 30 '22 20:09

Naphtali Visser