开发者

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 '开发者_如何学Crubygems' 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.

P.S. 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜