33

Is there an equivalent in Rails to PHP's die()?

I have a case where it would make my controller code a little cleaner to set it up in a way that would sometimes call render twice, unless there is a die() that is.

tybro0103
  • 48,327
  • 33
  • 144
  • 170

2 Answers2

56
abort("Message goes here")

See: How to create an exit message

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 1
    A better answer is `render :text => "Message goes here"` – skcin7 Nov 09 '12 at 07:16
  • 5
    I won't pretend to be a Ruby expert, but according to [this article](http://guides.rubyonrails.org/layouts_and_rendering.html) it seems that `render` is inappropriate as an equivalent to PHP's `die()` because execution continues after `render` is called. – George Cummins Nov 10 '12 at 09:00
1

A direct mapping of PHP's die() in Ruby would be Kernel::exit()

Christopher Manning
  • 4,527
  • 2
  • 27
  • 36
  • 1
    Er, it really isn't. `Kernel::exit()` raises a SystemExit exception, an exception which can be caught and ignored. While PHP's `die()` does permit destructors to run, this is a different circumstance and calling it a direct mapping is wholly inaccurate. – Winfield Trail Nov 07 '14 at 19:05