简体   繁体   English

在rails控制器中需要ruby gems

[英]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: 但如果我试图添加require 'rubygems'require 'mechanize'到Rails控制器,服务器给出:

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

I use RVM on Ubuntu 10.04 server machnine. 我在Ubuntu 10.04服务器machnine上使用RVM。 Ruby version: 1.9.2, Rails version: 3.0.3. Ruby版本:1.9.2,Rails版本:3.0.3。 Server: Passanger under Apache2. 服务器:Apache2下的Passanger。

PS If I run rails server and go to mysite.com:3000 all works without any error, so there is a problem with Passanger! PS如果我运行rails server并转到mysite.com:3000所有工作没有任何错误,所以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 这就是为什么Bundler被添加到Rails 3.只需将机械化添加到你的Gemfile就像这样

gem "mechanize"

and run 并运行

bundle install

on the command line. 在命令行上。 Any gem mentioned here will be required on application startup. 应用程序启动时将需要此处提到的任何gem。

The way you manage dependencies in Rails 3, is using the Gemfile and Bundler . 在Rails 3中管理依赖项的方法是使用Gemfile和Bundler

Edit your Gemfile and add 编辑Gemfile并添加

gem "mechanize"

Then run 然后跑

$ bundle install

Restart the server. 重启服务器。 The library will automatically be loaded. 库将自动加载。 No need to manually require RubyGems. 无需手动需要RubyGems。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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