never_had_a_name
never_had_a_name

Reputation: 93296

error messages in rails?

how do i display error messages in the web browser when using rails?

eg. if i use a variable in the view that i hasn't defined in the controller i want to get an error message.

Upvotes: 2

Views: 267

Answers (2)

Jim Schubert
Jim Schubert

Reputation: 20367

In your controller:

def your_method
  #processing that fails
  flash[:notice] = 'your error message'
end

In your view:

<% if !flash[:notice].nil? %>
    <p id="notice"><%= flash[:notice] %></p>
<% end %>

The documentation for the Flash hash is available here.

To rescue from errors at the application level rather than displaying error messages to the user, you can use

  rescue_from ErrorType, :with => :action_method

Examples:
Customising the generic Rails error message
http://www.perfectline.co.uk/blog/custom-dynamic-error-pages-in-ruby-on-rails

Upvotes: 3

dombesz
dombesz

Reputation: 7909

When you have errors like the mentioned one, when you try to reach the respective page you should have an error message instead of the proper page.

Upvotes: 0

Related Questions