1

I am relatively new to Ruby on Rails, but I have almost completed the online course on Lynda. I am stuck on 14-5 "Using the positionMove module". The tutorial is in Rails 3 while my version is Rails 4. I have the solution code, however it does not work for me. I have put my file position_mover.rb in the lib directory. Then in my subject model I require and include the module like this:

require 'lib/position_mover'
class Subject < ActiveRecord::Base

  include PositionMover
  ...
end

I used the methods in this model in the Subject controller just like the instructor. However, when I go to run my app on the Subjects index page I get the error:

cannot load such file -- lib/position_mover

app/models/page.rb:1:in `<top (required)>'
app/views/subjects/list.html.erb:23:in `block in_app_views_subjects_list_html_erb__420960863_60005508'
app/views/subjects/list.html.erb:18:in `_app_views_subjects_list_html_erb__420960863_60005508'
app/controllers/subjects_controller.rb:9:in `index'

I have tried many different approaches that I found online such as adding it to the class definition (Ruby on Rails 4.0 - Loading and using a module) or moving it to a different directory and specifying the path. How do I achieve the same result in Rails 4? What am I doing wrong? Any help is greatly appreciated.

Community
  • 1
  • 1
klib
  • 697
  • 2
  • 11
  • 27

1 Answers1

1
require "#{Rails.root}/lib/position_mover" 

or

require_relative 'lib/position_mover'

You also can auto-loading lib files.

in config/application.rb:

config.autoload_paths << Rails.root.join('lib')
pangpang
  • 8,581
  • 11
  • 60
  • 96
  • Thanks for the suggestions but I have tried all of those options and I still get the same error every time. – klib Apr 30 '15 at 05:42
  • Can you tell me the path of lib directory? – pangpang Apr 30 '15 at 05:52
  • it is root/lib/position_mover.rb – klib Apr 30 '15 at 05:54
  • It turns out I left a require statement in my page.rb and section.rb files. I added lib to the autoload path in the config/application.rb like you suggested and needed to remove all my require statements from my other models and now the app works. – klib Apr 30 '15 at 06:34