0

我想在 Sinatra 中实现这条特定的路线:

post '/activate/:model/:id/?' do
model.activate stuff

基本上这里发生的是我使用Module.const_get. 然后我在对象上调用该activate方法。现在我想添加一个 deactivate 方法,我可以复制代码并将其修改为 deactivate,但它不是很干燥。我可能应该使用正则表达式,对吧?我如何“动态调用方法”(又名调用停用或激活取决于路线?

4

1 回答 1

0

一种方法是使用助手和对象send方法。

module Sinatra
  module ActivateDiactivate
    def activate_diactivate(route)
      activ_deactiv = route.split("/")
      post route do
       model = Module.const_get(activ_deactiv[1].to_sym)
       model.send(method, stuff)
      end
    end
  end
  register AcivateDiactivate
end


activate_diactivate '/activate/:model/:id/?'
activate_diactivate '/diactivate/:model/:id/?'
于 2013-09-18T06:51:17.173 回答