开发者

How to require a class file in Rails 3 /lib directory from another class in /lib

I am new to Ruby and Rails and am having trouble getting a class in /lib to reference another class in /lib.

I have this in my application.rb file, as I understand Rails 3 does not auto-load the lib directory:

    config.autoload_paths 开发者_运维百科+= %W(#{config.root}/lib)

Then I have lib/Rec.rb

class Rec
  Movie @movie
...

and then lib/Movie.rb. I am able to instantiate a Movie object in a controller, but referencing in Rec produces:

undefined method `Movie' for Rec:Class
...
lib/rec.rb:2:in `<class:Rec>'
lib/rec.rb:1:in `<top (required)>'


Your code is...messed up. What do you mean Movie @movie? What exactly do you want to do?

That piece of code is invalid Ruby unless you have declared Movie as a method somewhere. But Movie should be a class, right? So that's the first reason Rails is not loading your movie.rb file, because Ruby is confused about what your code means.

Maybe what you mean is

@movie = Movie.new

Something like that should autoload your movie.rb, because now the code makes sense.

But..

Movie @movie

..doesn't mean anything..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜