Forum: Ruby on Rails Help with exceptions in UI

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A7b953c67b37077eea03e1dd183519e4?d=identicon&s=25 onemind (Guest)
on 2007-04-22 22:52
Hi Ruby forum,
I'm pretty new to Rails and Ruby and I'm trying to figure out how to
avoid having ugly stack dumps in my applications. I'm trying build a
Login module following the "Rails Recipes". It works fine when users
enter correct data, but of course, when the fields are blank there's an
exception in the UI.

My question is: how do I handle/catch the exception so that users won't
see the rails messages? I just want to drop them off on the page they
were on with a "you're being naughty" message.

Here is my model code:
  def self.authenticate(username, password)
    user = User.find(:first, :conditions => ['username = ?', username])
    if user.blank? ||
      Digest::SHA256.hexdigest(password + user.password_salt) !=
      raise "Username or password invalid"
And controller:
def signin
      session[:user] = User.authenticate(params[:username],
      redirect_to :action => session[:intended_action],
                      :controller => session[:intended_controller]
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-04-23 00:01
(Received via mailing list)
Two possible ideas:

a) Put begin/rescue around the call to authenticate in the controller;
if exception is thrown, reload page with error message.

b) Change authenticate to return nil intead of throwing; then just check
if you got a user back in your controller.

By the way, rails only shows stack traces in development mode. In
production the user would just get a 500 page... still not what you
want, but you don't have to worry about stack traces being shown to the

Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-04-23 05:09
(Received via mailing list)
onemind wrote:

>    user = User.find(:first, :conditions => ['username = ?', username])

Always challenge code that raises exceptions - there are usually ways
it. Specifically, in a pinch, that could be this:

   user = User.find(:first, :conditions => ['username = ?', username])
rescue nil

(All on one line.)

That has the added benefit of hiding any kind of error, not just "user
found" errors. Because you probably don't need that benefit, you can
things shorter and better at the same time:

   user = User.find_by_username(username)

Any find_by_* method returns 'nil' if the target ain't found.

  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax
This topic is locked and can not be replied to.