[Rails] accessing module from controller

1,018 views
Skip to first unread message

Dave Preschel

unread,
Apr 26, 2010, 6:27:02 PM4/26/10
to rubyonra...@googlegroups.com
I know this is probably something simple that I'm missing. I am trying
to call a function that is in a module in /lib, but the controller
doesnt seem to be finding it.

I have:

lib/siteopen.rb:

module Siteopen
def is_site_open()
return 1
end
end

and in a controller:

class StartupController < ApplicationController
require 'Siteopen'

def index
if is_site_open()
@sitemsg="The site is now open"
else
@sitemsg="Site is closed."
end
end
end


This will give me the following error:

NoMethodError in StartupController#index
undefined method `is_site_open' for #<StartupController:0x1045b2ea8>

I'm sure it's something dumb I'm doing.
Thanks..
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Frederick Cheung

unread,
Apr 26, 2010, 6:30:18 PM4/26/10
to Ruby on Rails: Talk


On Apr 26, 11:27 pm, Dave Preschel <li...@ruby-forum.com> wrote:
> I know this is probably something simple that I'm missing. I am trying
> to call a function that is in a module in /lib,  but the controller
> doesnt seem to be finding it.
>
If you want to be able to use the methods from a module like that you
need to include the module (or extend it if you just want to add the
methods to one instance). require (which is unnecessary here) just
asks ruby to load the file.

Fred

> I have:
>
> lib/siteopen.rb:
>
> module Siteopen
>   def is_site_open()
>     return 1
>   end
> end
>
> and in a controller:
>
> class StartupController < ApplicationController
>   require 'Siteopen'
>
>   def index
>      if is_site_open()
>          @sitemsg="The site is now open"
>      else
>          @sitemsg="Site is closed."
>       end
>    end
>  end
>
> This will give me the following error:
>
> NoMethodError in StartupController#index
> undefined method `is_site_open' for #<StartupController:0x1045b2ea8>
>
> I'm sure it's something dumb I'm doing.
> Thanks..
> --
> Posted viahttp://www.ruby-forum.com/.

Dave Preschel

unread,
Apr 26, 2010, 6:44:14 PM4/26/10
to rubyonra...@googlegroups.com
Frederick Cheung wrote:
> If you want to be able to use the methods from a module like that you
> need to include the module (or extend it if you just want to add the
> methods to one instance). require (which is unnecessary here) just
> asks ruby to load the file.
>
> Fred

That was it!
Thanks,Dave

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages