【问题标题】:Rails: Syntax error using paperclip and aws on herokuRails:在heroku上使用回形针和aws的语法错误
【发布时间】:2017-05-16 19:01:09
【问题描述】:

我正在使用回形针、devise、aws 和 heroku 在我的 Rails 应用程序上显示我的图片。在我更改我的 app/assets/application.js 和我的 controllers/users/sessions_controller.rb 之前它工作正常

注意:我没有更改 config/environments/production.rb 中的任何内容

但是,当我使用 heroku run rails 控制台时,我收到一条语法错误消息。

Running rails console on ⬢ immense-spire-90312... up, run.8450 (Free)
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require': /app/config/environments/production.rb:88: syntax error, unexpected '\n', expecting => (SyntaxError)
/app/config/environments/production.rb:92: syntax error, unexpected ':', expecting keyword_end
storage: :s3,
        ^

在我的 config/environments/production.rb 中

config.paperclip_defaults = {
  Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
  Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
  Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-central-1.amazonaws.com'

  # The syntax error seems to be here: 
  storage: :s3,
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

我改变了什么:

在 javascripts/application.js 中,我将 //= require bootstrap 放在 jquery_ujs 下(一开始它在底部)

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require owl.carousel
//= require_tree .

我在设计中创建了一个会话控制器,所以当用户登录时,我想更改数据库中的某些内容

class Users::SessionsController < Devise::SessionsController

  def create
    super do |user| 
      user.randomized_fakeposts.delete_all
      # Note: A fakeposts consists of an image_url which is taken from aws.
      Fakepost.all.each do |fp|
        user.randomized_fakeposts.new(fakepost: fp)
      end
      user.save
    end
  end
end

【问题讨论】:

  • 无论您记得更改了什么,从您的production.rb 中摘录的代码都不是有效的 Ruby:您不能在哈希声明中使用=,除非它是字符串的一部分或价值声明。
  • @coreyward:我觉得很愚蠢,就是这样(维杰和你有同样的解决方案)。谢谢你。然而,代码之前工作仍然很奇怪,因为我没有接触production.rb。不过,谢谢:)。
  • 可能是对 production.rb 的更改之前已进行但未保存(和/或未提交)。这是我在提交之前总是查看差异的原因之一,并仔细检查git status,即使它是单个字符被更改。 #protip
  • @coreyward:呵呵,谢谢,我会记住的:)。

标签: ruby-on-rails amazon-web-services heroku


【解决方案1】:

我查看了您的代码,发现这是您的 production.rb 代码的语法错误。

您正在使用哈希,但最后错过了逗号(,)。所以请用以下几行替换您的代码。

config.paperclip_defaults = {
  :url => ':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename',
  :s3_host_name] => 's3-eu-central-1.amazonaws.com',

  # The syntax error seems to be here: 
  storage: :s3,
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

【讨论】:

  • 就是这样,谢谢:)。然而,我的代码以前可以工作对我来说仍然有点奇怪(我发誓我没有更改production.rb,当它工作时)。尽管如此,它现在可以工作了,所以谢谢你:)!
猜你喜欢
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2021-04-21
  • 1970-01-01
  • 2013-04-15
  • 2017-06-24
相关资源
最近更新 更多