确定原始控制者
我的设置:Rails 3.0.9,Ruby 1.9.2
我正在使用 Cancan 来授权控制器操作。如果用户指定丢失的ID,那么我有以下代码
application_controller.rb
rescue_from ActiveRecord::RecordNotFound do |exception|
flash[:alert] = "Oops, I cannot find this record, please try again."
respond_to do |format|
format.html { redirect_to root_url }
end
end
我希望上面的代码将闪现消息设置为
flash[:alert] = "Oops, I cannot find this person, please try again."
“人”之类的东西,在这种情况下可以是任何模型,例如,如果用户尝试要访问某个国家/地区丢失的 ID,它应该显示“
flash[:alert] = "Oops, I cannot find this country, please try again".
你明白了”。我想我应该能够抓住原始呼叫和控制器,有人知道如何做到这一点或者有更好的方法吗?
My setup: Rails 3.0.9, Ruby 1.9.2
I am using Cancan to authorize a controller action. If the user specify a missing id, then I have the following code
application_controller.rb
rescue_from ActiveRecord::RecordNotFound do |exception|
flash[:alert] = "Oops, I cannot find this record, please try again."
respond_to do |format|
format.html { redirect_to root_url }
end
end
What I would like is for the above code to set the flash message to something like
flash[:alert] = "Oops, I cannot find this person, please try again."
"person" in this case could be any of the model, e.g, if the user tried to access a missing id for a country, it should say
flash[:alert] = "Oops, I cannot find this country, please try again".
You get the idea. I'm thinking I should be able to grab the originating call and controller, does anyone know how to do this or has a better way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以做的一件事是解析
exception.message
它通常包含一个看起来像这样的字符串“无法找到 ID=03 [WHERE images.state = 'published']”的图像
,但我会使用
params
对象访问控制器和导致错误的操作,如下所示:
干杯
one thing you could do is to parse
exception.message
which usally contains a string looking like this"Couldn't find Image with ID=03 [WHERE images.state = 'published']"
but I would use the
params
object to access the controller and action causing the errorlike this:
cheers