سؤال

I have a module (cgi_helper.rb), a cgi that calls that module, and an html template. I get the following error:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in require': cannot load such file -- cgi_helper (LoadError) from >/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire' from lab4.cgi:8:in `'

The Module: http://pastebin.com/YFj0rC8p

The CGI: http://pastebin.com/xs5LiV2a

I can't figure out why it can't find the module. It's executable and in the same dir as the cgi. Here is the erb template.

<table>
<tr>
<th>Number</th>
<th>Username</th>
<th>Passwd</th>
<th>UID</th>
<th>GID</th>
<th>Home Dir</th>
<th>Shell</th>
<% students.each do |x| %>
<% next if eachname[x].nil? %>
<% s = Student.new(eachname[x].split(':')) %>
<tr>
<% columns.each do |c| %>
<td><%= s.send(c).to_s %></td>
<% end %>
</tr>
<% end %>
</table>
<% finish = Time.now %>
<h2> Total Elapsed Time:<%= (finish.to_f - start.to_f).to_s %></h2>
هل كانت مفيدة؟

المحلول 2

You should change

require 'cgi_helper'

to

require File.expand_path(File.dirname(__FILE__) + '/cgi_helper')

or put unshift on the top of the script

$:.unshift File.dirname(__FILE__)

require 'erb'
require 'cgi_helper'
include CgiHelper

Detailed info about require.

نصائح أخرى

First off: require doesn't load modules, it loads files.

require loads a file from the $LOAD_PATH, if the directory your cgi_helper.rb file is in isn't on the $LOAD_PATH, then it won't be found. It should be the job of the package management system (e.g. RubyGems) to adjust the $LOAD_PATH so that your files can be found.

If you want to load a file relative to the location of the file that is doing the loading, then you should use require_relative:

require_relative 'cgi_helper'

Manually fiddling with the $LOAD_PATH hasn't been necessary for ages.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top