【问题标题】:Rails 4: Unable to protect data using Figaro gemRails 4:无法使用 Figaro gem 保护数据
【发布时间】:2014-07-27 03:09:56
【问题描述】:

使用 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 并将以下信息添加到 application.yml

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

删除 = 后:现在又出现错误

Users/neilpatel/Desktop/Rails/tweetscope/app/models/keyword.rb:10:语法错误,意外的 tCONSTANT,期待关键字_end ...g.access_token_secret:' /Users/neilpatel/Desktop/Rails/tweetscope/app/models/keyword。 rb:34: 语法错误,意外的输入结束,期待关键字结束

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

【问题讨论】:

  • 您是否尝试过按照SO answer 中的建议在application.yml 中使用: 而不是=?
  • 是的,现在出现新错误

标签: ruby-on-rails ruby heroku


【解决方案1】:

当您从database.yml 之类的地方访问值时,您需要&lt;%= ... %&gt; 语法。但是当你在一个模型中时,你应该把它排除在外。试试:

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

【讨论】:

    【解决方案2】:

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

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

    I.E.使用 Javascript 对象表示法。你改用=,我认为这就是导致 figaro 崩溃的原因。

    【讨论】:

    • 嗨,谢谢,试过了,但收到了我更新的错误消息
    猜你喜欢
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多