Forum: Ruby on Rails Use constants from module in view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8f72e436a3107703e6ee950512036598?d=identicon&s=25 shenry (Guest)
on 2009-05-28 09:17
(Received via mailing list)
I have a constant.rb file in lib/ with:

module Constants
  FOO = %w( tastes great)
  BAR = %w( less filling)
end

I'm able to access the constants fine in my Model, but I'm also using
them to populate select boxes in a view and can't figure out how to
make them available to the view.

I've tried:

require 'constants'
class ApplicationController < ActionController::Base
  include Constants
  ....

end

as well as the same thing in the specific controller whose method is
calling the offending action.

Any advice on where to require the module? Thanks,

Stu
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-28 10:21
(Received via mailing list)
On May 28, 8:17 am, shenry <stuarthe...@gmail.com> wrote:
>
> calling the offending action.
>
> Any advice on where to require the module? Thanks,
It's not to do with requiring (you can get rid of that require
'constants') it's to do with where the module is included.
You could just write Constants::FOO in your view.

Alternatively,

class ApplicationController < ActionController::Base
  include Constants
  helper Constants
end

might work (but slightly abusing the helper method)

Fred
8f72e436a3107703e6ee950512036598?d=identicon&s=25 shenry (Guest)
on 2009-05-28 16:56
(Received via mailing list)
> You could just write Constants::FOO in your view.

Thanks Fred, the syntax above works great.

Stu
This topic is locked and can not be replied to.