确定原始控制者

发布于 2024-11-18 10:45:04 字数 701 浏览 5 评论 0原文

我的设置: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-11-25 10:45:04

你可以做的一件事是解析 exception.message 它通常包含一个看起来像这样的字符串

“无法找到 ID=03 [WHERE images.state = 'published']”的图像

,但我会使用
params 对象访问控制器和导致错误的操作,

如下所示:

    flash[:alert] = "Oops, I cannot find this #{params[:controller].upcase.singularize}, please try again."

干杯

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 error

like this:

    flash[:alert] = "Oops, I cannot find this #{params[:controller].upcase.singularize}, please try again."

cheers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文