Problem accessing mixin module methods from views

I’m trying to write a module to be used as a mix-in, my first attempt at
such. This sample code outlines the basics of what I’ve done so far…

/lib/foo.rb:
require ‘foo/base’
module Foo
include FooBase
end

/lib/foo/base.rb:
module Foo
module FooBase
def foo_var
123
end
end
end

/app/controllers/application.rb

Filters added to this controller apply to all controllers in the

application.

Likewise, all the methods added will be available for all controllers.

require ‘foo’
class ApplicationController < ActionController::Base
include Foo
end

Given these three files, I can use “foo_var” without error in any of my
controllers. However, when I try to use “foo_var” in a view, I get a “no
method” error. I know it’s possible to make “foo_var” available
everywhere - example, the login engine had “current_user” and “user?”
methods that could be accessed in any controller or view.

I’m sort of shooting in the dim light, not quite the dark, after reading
some articles and sample code. Any help appreciated.

thanks.

Hai cayce,

Once you include a module say Foo, only methods from Foo will be mixed
to the included class.
But i am not sure whether the nested module’s will also get mixed to
the included class. If yes, not sure how to access those methods.

But in your example if you add
require ‘foobase’
class ApplicationController < ActionController::Base
include Foo
include FooBase

end

now if you say

ApplicationController.new.foo_var, surely it will work.

On Aug 28, 7:58 pm, Cayce B. [email protected]

Thanks I’ll give that a shot.

c.

raghukumar wrote:

Hai cayce,

Once you include a module say Foo, only methods from Foo will be mixed
to the included class.
But i am not sure whether the nested module’s will also get mixed to
the included class. If yes, not sure how to access those methods.

But in your example if you add
require ‘foobase’
class ApplicationController < ActionController::Base
include Foo
include FooBase

end

now if you say

ApplicationController.new.foo_var, surely it will work.

On Aug 28, 7:58 pm, Cayce B. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs