本文介绍了如果找不到记录,Rails 如何重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果未找到记录,我将尝试重定向.页面未重定向,我未找到错误记录.
I am trying to redirect if the record is not found.The page is not redirect and I get the error record not found.
我的控制器:
def index
@link = Link.find(params[:id])
respond_to do |format|
if @link.blank?
format.html { redirect_to(root_url, :notice => 'Record not found') }
else
format.html { render :action => "index" }
end
end
end
推荐答案
我一直在做的是把这个放在方法的末尾:
What I've been doing is putting this at the end of the method:
rescue ActiveRecord::RecordNotFound
redirect_to root_url, :flash => { :error => "Record not found." }
更好的是,把它作为你的控制器的 around_filter:
Even better, put it as an around_filter for your controller:
around_filter :catch_not_found
private
def catch_not_found
yield
rescue ActiveRecord::RecordNotFound
redirect_to root_url, :flash => { :error => "Record not found." }
end
这篇关于如果找不到记录,Rails 如何重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!