我对Rails还比较陌生,但我几乎已经完成了Lynda的在线课程。我被困在14-5“使用positionMove模块”。本教程在Rails 3中,而我的版本是Rails 4。我有解决方案代码,但是它对我不起作用。我已经将我的文件position_mover.rb放在lib目录中。然后,在我的主题模型中,我需要并包括这样的模块:
require 'lib/position_mover'
class Subject < ActiveRecord::Base
include PositionMover
...
end我在学科控制器中使用了这个模型中的方法,就像讲师一样。但是,当我在Subject索引页面上运行我的应用程序时,我会得到以下错误:
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'我尝试过许多我在网上找到的不同方法,比如将它添加到类定义(Ruby on Rails 4.0 - Loading and using a module)中,或者将它移到不同的目录中,并指定路径。如何在Rails 4中实现相同的结果?我做错了什么?任何帮助都是非常感谢的。
发布于 2015-04-30 01:52:23
require "#{Rails.root}/lib/position_mover" 或
require_relative 'lib/position_mover'您还可以自动加载库文件。
在config/application.rb中
config.autoload_paths << Rails.root.join('lib')https://stackoverflow.com/questions/29957561
复制相似问题