图像未正确关联(载波、雾、heroku)

发布于 2024-12-05 02:20:43 字数 2182 浏览 0 评论 0原文

尝试将基于载波的图像上传添加到我的用户模型中。这在本地开发中有效。在heroku上,图像已上传到我的s3存储桶,但从未显示。在控制台中,看起来没有上传图像。

如果图像正在进入 s3,为什么它没有正确关联?

这是代码:

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|

  if Rails.env.production?
    config.fog_credentials = {
      provider:              'AWS',               # required
      aws_access_key_id:     ENV['S3_KEY'],       # required
      aws_secret_access_key: ENV['S3_SECRET']     # required
    }

    config.fog_directory  = ENV['S3_BUCKET']      # required
    config.fog_public     = false                 # optional, defaults to true
    config.fog_attributes = {
      'Cache-Control'=>'max-age=315576000'
    }

    config.storage = :fog
  else
    config.storage = :file
  end

end

# app/uploaders/profile_picture_uploader.rb
class ProfilePictureUploader < CarrierWave::Uploader::Base

  def store_dir
    "uploads/#{ model_name }/#{ mounted_as }/#{ model.id }"
  end

  def cache_dir
     Rails.root.join *%w[ tmp uploads model_name ]
  end

  def extension_white_list
    %w[ jpg jpeg gif png ]
  end

private

  def model_name
    @model_name ||= model.class.to_s.underscore
  end

end

# app/models/user.rb
class User < ActiveRecord::Base

  mount_uploader :profile_picture, ProfilePictureUploader

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  attr_accessible :email, :password, :password_confirmation, :profile_picture

end

# app/views/users/edit.html.haml
%h1 Edit user

= form_for @user do |f|

  %h2 Profile Picture
  - if f.object.profile_picture.present?
    = image_tag f.object.profile_picture
    %p= f.object.profile_picture.file.basename

    = f.check_box :remove_profile_picture
    = f.label :remove_profile_picture, 'Delete your Profile Picture'

    = f.label :profile_picture, 'Replace your Profile Picture'
  - else
    = f.label :profile_picture, 'Add your Profile Picture'
  = f.file_field :profile_picture

  %p
    = f.submit 'Save'
    or
    = link_to 'cancel', @user

平台:

  • rails 3.1.0
  • Carrierwave 0.5.7
  • heroku cedar stack

Trying to add carrier wave based image uploading to my user model. This works locally in development. On heroku, the image is uploaded to my s3 bucket, but never displayed. In the console, it looks like no image was uploaded.

If the image is making it to s3, why isn't it being associated properly?

Here's the code:

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|

  if Rails.env.production?
    config.fog_credentials = {
      provider:              'AWS',               # required
      aws_access_key_id:     ENV['S3_KEY'],       # required
      aws_secret_access_key: ENV['S3_SECRET']     # required
    }

    config.fog_directory  = ENV['S3_BUCKET']      # required
    config.fog_public     = false                 # optional, defaults to true
    config.fog_attributes = {
      'Cache-Control'=>'max-age=315576000'
    }

    config.storage = :fog
  else
    config.storage = :file
  end

end

# app/uploaders/profile_picture_uploader.rb
class ProfilePictureUploader < CarrierWave::Uploader::Base

  def store_dir
    "uploads/#{ model_name }/#{ mounted_as }/#{ model.id }"
  end

  def cache_dir
     Rails.root.join *%w[ tmp uploads model_name ]
  end

  def extension_white_list
    %w[ jpg jpeg gif png ]
  end

private

  def model_name
    @model_name ||= model.class.to_s.underscore
  end

end

# app/models/user.rb
class User < ActiveRecord::Base

  mount_uploader :profile_picture, ProfilePictureUploader

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  attr_accessible :email, :password, :password_confirmation, :profile_picture

end

# app/views/users/edit.html.haml
%h1 Edit user

= form_for @user do |f|

  %h2 Profile Picture
  - if f.object.profile_picture.present?
    = image_tag f.object.profile_picture
    %p= f.object.profile_picture.file.basename

    = f.check_box :remove_profile_picture
    = f.label :remove_profile_picture, 'Delete your Profile Picture'

    = f.label :profile_picture, 'Replace your Profile Picture'
  - else
    = f.label :profile_picture, 'Add your Profile Picture'
  = f.file_field :profile_picture

  %p
    = f.submit 'Save'
    or
    = link_to 'cancel', @user

Platform:

  • rails 3.1.0
  • carrierwave 0.5.7
  • heroku cedar stack

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-12-12 02:20:43

仍然不完全确定为什么这有效,但我在我的模型中添加了一个方法

def profile_picture_file_name
  read_attribute :profile_picture_file
end

,并在我的视图中使用该方法而不是

f.object.profile_picture.file.basename

现在一切都很高兴。

感谢在视图中显示 Carrierwave 文件名

Still not entirely sure why this works, but I added a method to my model

def profile_picture_file_name
  read_attribute :profile_picture_file
end

and use that method in my view instead of

f.object.profile_picture.file.basename

And everything is happy now.

Thanks to Displaying a Carrierwave filename in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文