ruby-on-rails - rails : checking which controller method was called from within the model

标签 ruby-on-rails ruby

有没有办法检查从模型中调用了哪个 Controller 方法?

例子: 假设调用了 Controller 创建方法:

def create
     do something
end

然后在模型中只在调用 Controller 中的创建时才做一些事情

if create?
      do something
end

最佳答案

我想您可以检查调用堆栈,但这正是模型的目的:它们现在不应该与 Controller 有关。

检查堆栈:

if caller.grep /create/
  # do something
elsif caller.grep /update/
  #do something else
end

应该可以解决问题。

关于ruby-on-rails - rails : checking which controller method was called from within the model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925852/

相关文章:

ruby-on-rails - rails : Runtime configuration of ActionMailer?

ruby-on-rails - Rails API : params' tempfile contains null bites from upload and fails to read, 但如果在本地抓取则读取正常

ruby - 带有 Ruby 的 OpenGL 3.1+

ruby-on-rails - 创建一种方法或范围来显示较低的价格?

ruby-on-rails - Github:克隆存储库、对克隆进行更改并提交到原始存储库

ruby-on-rails - 如何检查我的 heroku 应用程序正在使用哪个数据库?

ruby-on-rails - 如何在 ruby​​ on rails 中通过 websocket 发送一个 keep-alive 数据包

ruby - 通过 Thread.list.size 得到的线程数和通过/proc 得到的线程数不一样

ruby - Rails 3 多态关联,具有多个内容项的用户模型?

ruby-on-rails - 删除回形针图像 Active Admin