error messages in rails?
how do i display error messages in the web browser when using rails?
eg. if i use a va开发者_运维百科riable in the view that i hasn't defined in the controller i want to get an error message.
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
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.
精彩评论