在Rails 3 Controller中要求gem失败,并显示“ Constant Missing”(恒定丢失)。 [英] Requiring gem in Rails 3 Controller failing with "Constant Missing"

查看:42
本文介绍了在Rails 3 Controller中要求gem失败,并显示“ Constant Missing”(恒定丢失)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其他线程中也曾多次问过这个问题,但似乎没有一个答案。

I've seen this asked a few times in other threads, but none of the answers seem to apply.

环境:
Rails 3来自jugend的亚马逊/ ecs宝石。唯一的文件在这里:
http: //github.com/jugend/amazon-ecs/blob/master/lib/amazon/ecs.rb

我的gemfile有:
gem'amazon-ecs',:git =>'git://github.com/jugend/amazon-ecs.git'

my gemfile has: gem 'amazon-ecs', :git => 'git://github.com/jugend/amazon-ecs.git'

一切都在irb中起作用。我可以运行:
捆绑控制台
需要'amazon / ecs'然后去城镇

Everything works in irb. I can run: bundle console require 'amazon/ecs' and then go to town

当我尝试从控制器使用它时,例如:
需要'amazon / ecs'

when I try to use it from the controller though, like so: require 'amazon/ecs'

require 'amazon/ecs'

class SearchController < ApplicationController
  def index    
  end

  def results
    Amazon::Ecs.configure do |options|
        options[:aWS_access_key_id] = '[key]'
        options[:aWS_secret_key] = '[secret]'
    end

    res = Amazon::Ecs.item_search(params[:search], {:response_group => 'Medium', :search_index => 'All'})
  end
end

我在第8行获得了:未初始化的常量SearchController :: Amazon。我首先尝试使用Amazon。

I get: uninitialized constant SearchController::Amazon at line 8, where I first try to use Amazon.

ecs.rb具有包含类Ecs的Amazon模块。我不确定为什么它可以在erb中运行,而不是在Rails中运行。

the ecs.rb has a module Amazon containing a class Ecs. I'm not sure why this is working in erb, and not in rails.

我对Rails还是很陌生,所以请用小词回答。 :-/

I'm still kinda new to Rails, so please answer using small words. :-/

推荐答案

默认情况下,所有gem都需要其文件,因此通常不需要显式要求任何文件

All of the gems require their files by default, so usually you don't need to explicitly require any files.

说到您的问题,可能是在处理Amazon模块之前运行了您的控制器。

Speaking about your problem, it could somehow be, that your controller is run before Amazon module is processed.

这篇关于在Rails 3 Controller中要求gem失败,并显示“ Constant Missing”(恒定丢失)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆