gpt4 book ai didi

ruby-on-rails - 在 before_filter 中调用的模型级方法

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:02 27 4
gpt4 key购买 nike

我在 Rails 2 中工作,我有一个模型级方法,我想在 before_filter 中调用它。我怎样才能做到这一点?我试过这种方法,但它不起作用

before_filter :LmsUser.can_edit_update, :only => [:new, :create, :edit, :update, :destroy]

最佳答案

您应该向您的 Controller 添加一个方法并将其用作之前的过滤器。例如:

class MyController < ApplicationController
before_filter :check_permissions,
:only => [:new, :create, :edit, :update, :destroy]

private
def check_permissions
unless LmsUser.can_edit_update
# redirect_to, render, or raise
end
end
end

参见 filters section of the Action Controller Overview guide获取更多信息。

关于ruby-on-rails - 在 before_filter 中调用的模型级方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12889581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com