I have a constant defined in a Model, and need access to it from within a Helper. Is there a way to do this? Example:
#app/models/my_model.rb
class MyModel < ActiveRecord::Base
VALUE = 3
def self.get_value
VALUE
end
end
#app/helpers/users_helper.rb
module UsersHelper
VALUE_V1 = MyModel.get_value
VALUE_V2 = MyModel::VALUE
end
Both VALUE_V1, and VALUE_V2 fail for me (uninitialized constant).
You need to use top level namespace when referring a constant inside module.
So,
value1 = ::MyModel.get_value
Also, as Vimsha commented you can use the constant directly, no need to define another constant.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.