论坛首页 Ruby版 rails

如何在 find(params[:id] 报RecordNotFound错误后,跳转指定页面?

浏览 192 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-05-09
就是在action中,当传入的ID通过 model.find(params[:id])没找到时,比如用户手动输入的id。就会出现难看的ActiveRecord::RecordNotFound页面。
我现在的做法是在ApplicationController加了
around_filter :rescue_record_not_found

def rescue_record_not_found
  begin
    yield
  rescue ActiveRecord::RecordNotFound
    render :file => "#{RAILS_ROOT}/public/404.html"
  end
end

一般来说应该怎么处理这个问题?
   
时间:2008-05-09
http://api.rubyonrails.org/classes/ActionController/Rescue/ClassMethods.html#M000226
   
0 请登录后投票
时间:2008-05-10
改成
rescue_from ActiveRecord::RecordNotFound :with => :rescue_record_not_found

def rescue_record_not_found
  render :file => "#{RAILS_ROOT}/public/404.html"
end

很好用。谢谢~~
   
0 请登录后投票
时间:昨天
如果有新的异常,可以在用rescue_from ActiveRecord::RecordNotFound :with => :rescue_record_not_found 不会覆盖把?
可以继续添加吗?
例如:
rescue_from Mysql::Error :with => :rescue_mysql_error
def rescue_mysql_error
render :file => "#{RAILS_ROOT}/public/404.html"
end
这样可以吗?如果数据库突然关闭,或者连接不上,该怎么捕获此类异常
   
0 请登录后投票
论坛首页 Ruby版 rails

跳转论坛:
JavaEye推荐
    快速回复 引用上一条消息 (Alt+S)