开发者

Trying to implement a Module using namespaces

I am using Ruby on Rails 3 and I am trying to implement a Module using namespaces.

In my lib/ folder I have the authorization.rb file with in this:

module Authorizations
  def Authorizations.message
    return "flash_message"
  end
end

In my controller I have:

class Users::AccountsController < ApplicationController # 'Users' is the namespace
  include Authorizations

  def create
    ...
    flash.now[:notice] =  Authorizations.message
  end
end

When I run the create method I get this error:

NoMethodError (undefined method 'message' for Authorizations:Module)

What is wrong?开发者_开发技巧


In the module statement I also tryed these

def Authorizations::message
 ...

# or 

def message
  ...

and also those don't work.


The problem is in the RAILS_ROOT/config/application.rb. Just load the lib/ folder in this way:

config.autoload_paths += %W(#{config.root}/lib)


module Authorizations
  def message
    return "flash_message"
  end
end

And then

Authorizations::message

or if included in the class, just

... = message
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜