开发者

How to use a module method in rails lib folder for a specific view

hi, supposing i have a module in my rails lib folder with some method like below with a file name - my_module.rb:

my_module.rb

Module MyModule
  class SystemModule
    def some_method
        ...开发者_如何学编程some cool stuff...
    end
  end
end

if i wanted to use in my controller i would just call this:

require 'my_module'

MyModule::SystemModule.some_method

How can i make and call some_method(like a helper method) from a view template?Where do i require my_module.rb


Sorry but your code is messed up.

You cannot call an instance method on a class. So you either have to define some_method like

def self.some_method ...

or you need to call it like

MyModule::SystemModule.new.some_method

Then modules are a bit tricky. You need to have the whole directory structure right. You need to provide a my_module directory. E.g. $RAILS_ROOT/app/lib/my_module/ and in this module you need to insert your system_module.rb file.

You should read how ruby handles modules, because rails is just a framework and common ruby rules apply.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜