Rails 5 -如何从视图调用lib中的类方法



我正在构建一个应用程序来学习Rails,并试图理解Rails在这种情况下的基本概念(抱歉我的无知,我是新手):

当点击屏幕上的一个按钮(在一个表单中),我想执行一个特定的功能-在distiller.rb中称为set_documenttype的方法-而留在视图中。

根据Rails 5的"pick-axe"书,我把它放在/lib/distiller和:

  • DocumentsController.rb I have set:

    class DocumentsController < ApplicationController require "distiller/distiller"

  • application.rb我设置了config.autoload_paths += %W(#{config.root}/lib)

distiller.rb

class Distiller
    def set_documenttype(f)
        #=> do something here
    end
end

现在,我得到

noomethoderror in Documents#edit

为....未定义方法' link_to_set_documenttype'

谢谢你的帮助。

尝试helper_method : set_documenttype将控制器方法声明为视图可用的助手。

如果还是不行,请分享distiller.rb的源码

相关内容

最新更新