简体   繁体   中英

Require ruby gems in rails controller

require 'rubygems'
require 'mechanize'

agent = Mechanize.new
page = agent.get("http://google.com/")

This simple script works ok.

But if I'am trying to add require 'rubygems' and require 'mechanize' to the Rails controller, server gives:

LoadError in NewsController#find
no such file to load -- mechanize

I use RVM on Ubuntu 10.04 server machnine. Ruby version: 1.9.2, Rails version: 3.0.3. Server: Passanger under Apache2.

PS If I run rails server and go to mysite.com:3000 all works without any error, so there is a problem with Passanger!

Please, help me!

You shouldn't require gems in your controller. Thats why Bundler was added to Rails 3. Just add mechanize to your Gemfile like this

gem "mechanize"

and run

bundle install

on the command line. Any gem mentioned here will be required on application startup.

The way you manage dependencies in Rails 3, is using the Gemfile and Bundler .

Edit your Gemfile and add

gem "mechanize"

Then run

$ bundle install

Restart the server. The library will automatically be loaded. No need to manually require RubyGems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM