Rails 4:无法使用Figaro gem保护数据

时间:2014-07-26 16:12:04

标签: ruby-on-rails ruby heroku

使用twitter api创建应用程序,我有相关的代码 但是当我想使用figaro gem来保护我在输入rake figaro时遇到错误:heroku

rake figaro:heroku
rake aborted!
NoMethodError: undefined method `reject' for #<String:0x007ff074af1010>
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/figaro-0.7.0/lib/figaro.rb:39:in `flatten'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/figaro-0.7.0/lib/figaro.rb:17:in `env'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/figaro-0.7.0/lib/figaro/railtie.rb:7:in `block in <class:Railtie>'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.4/lib/active_support/lazy_load_hooks.rb:36:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.4/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.4/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.4/lib/active_support/lazy_load_hooks.rb:44:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.4/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.4/lib/rails/application.rb:67:in `inherited'
/Users/neilpatel/Desktop/Rails/tweetscope/config/application.rb:10:in `<module:Tweetscope>'
/Users/neilpatel/Desktop/Rails/tweetscope/config/application.rb:9:in `<top (required)>'
/Users/neilpatel/Desktop/Rails/tweetscope/Rakefile:4:in `<top (required)>'

keyword.rb文件

def grab_twitts
      client = Twitter::REST::Client.new do |config|
      config.consumer_key        = "2DZcdTw4p1m2yU18xxxxxxxxx"
      config.consumer_secret     = <%= ENV["CONFIG.CONSUMER_SECRET"] %>
      config.access_token        = "71875314-xPOGuWIU7CiDcR3Vx9xxxxxxxxxxxxxxxx"
      config.access_token_secret = <%= ENV["CONFIG.ACCESS_TOKEN_SECRET"] %>
    end

安装figaro gem&amp;将以下信息添加到application.yml

CONFIG.CONSUMER_SECRET     = "whpCvjNmePXzj6iSfxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CONFIG.ACCESS_TOKEN_SECRET = "1P00CJmiQ7Tqhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

删除= to:现在得到aonther错误

Users / neilpatel / Desktop / Rails / tweetscope / app / models / keyword.rb:10:语法错误,意外tCONSTANT,期待keyword_end ... g.access_token_secret:&lt;%= ENV [“CONFIG.ACCESS_TOKEN_SECRET”。 .. ... ^ /Users/neilpatel/Desktop/Rails/tweetscope/app/models/keyword.rb:10:语法错误,意外'&gt;' /Users/neilpatel/Desktop/Rails/tweetscope/app/models/keyword.rb:34:语法错误,意外的输入结束,期待keyword_end

class Keyword < ActiveRecord::Base

    has_many :tweets

    def grab_twitts
      client = Twitter::REST::Client.new do |config|
      config.consumer_key "2DZcdTw4pvccccccccccccc"
      config.consumer_secret <%= ENV["CONFIG.CONSUMER_SECRET"] %>
      config.access_token "71875314-xPOGuWIU7CiDcR3Vx9pkJbbbbbbbbbbbbb"
      config.access_token_secret:  <%= ENV["CONFIG.ACCESS_TOKEN_SECRET"] %>
    end

    client.search(self.word, :count => 10, :result_type => "recent").take(10).collect do |tweet|
        new_tweet = Tweet.new

        new_tweet.tweet_id = tweet.id.to_s
        new_tweet.tweet_created_at = tweet.created_at
        new_tweet.text = tweet.text


        new_tweet.user_uid = tweet.user.id
        new_tweet.user_name = tweet.user.name
        new_tweet.user_screen_name = tweet.user.screen_name
        new_tweet.user_image_url = tweet.user.profile_image_url.to_s


        new_tweet.keyword = self

        new_tweet.save

        end
    end
end

2 个答案:

答案 0 :(得分:2)

当您从database.yml等地方访问值时,您需要<%= ... %>语法。但是当你在一个模特中时,你应该把它留下来。尝试:

client = Twitter::REST::Client.new do |config|
  config.consumer_key = "2DZcdTw4pvccccccccccccc"
  config.consumer_secret = ENV["CONFIG.CONSUMER_SECRET"]
  config.access_token = "71875314-xPOGuWIU7CiDcR3Vx9pkJbbbbbbbbbbbbb"
  config.access_token_secret = ENV["CONFIG.ACCESS_TOKEN_SECRET"]
end

答案 1 :(得分:1)

application.yml(和一般的YAML文件)键值对需要格式化如下:

CONFIG.CONSUMER_SECRET: "whpCvjNmePXzj6iSfxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CONFIG.ACCESS_TOKEN_SECRET: "1P00CJmiQ7Tqhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

即。使用Javascript对象表示法。您正在使用=代替,而且我认为这导致了figaro的破坏。