开发者

error messages in rails?

开发者 https://www.devze.com 2023-01-05 02:14 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消