Reputation: 93296
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
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
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