使用Paperclip

时间:2017-01-29 03:19:22

标签: ruby-on-rails ruby heroku amazon-s3 paperclip

在生产和开发方面,我似乎无法让AWS-S3使用我的Ruby应用程序。我可以将文件上传到我的S3 Bucket,但是如何显示它们?我将此用于用户个人资料头像,以便用户可以上传自己的头像。

这是我当前的图片标记:

<%= image_tag @user.profile.avatar.url, class: 'user-show-avatar' %>

我将我的头像上传信息放在个人资料模型中,这是我拥有头像信息的地方。

profile.rb

class Profile < ActiveRecord::Base
  belongs_to :user
  has_attached_file  :avatar, 
                    :styles => { :medium => "460x>", :thumb => "100x100>",:vnice=> "400x" },
                    :storage => :s3,
                    :bucket => 'mybucket',
                    :s3_credentials => "#{Rails.root}/config/aws.yml",
                    :path => "resources/:id/:style/:basename.:extension"

    validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

end

_form.html.erb(编辑个人资料表格)

<%= form_for @profile, url: user_profile_path, :html => { :multipart => true } do |f| %>
        <div class="form-group">
          <%= f.label :avatar %>
          <%= f.file_field :avatar, class: 'form-control' %>
        </div>
<% end %>

配置/初始化/ paperclip.rb

Paperclip::Attachment.default_options[:storage] = :s3

Paperclip::Attachment.default_options[:s3_credentials] = {
  :bucket => ENV['AWS_BUCKET'],
  :access_key_id => ENV['AWS_KEY'],
  :secret_access_key => ENV['AWS_SECRET_KEY'],
  s3_region: 'us-east-1'
}

Paperclip::Attachment.default_options[:s3_options] = {
  endpoint: 'https://objects-us-east-1.io'
}

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] = 'objects-us-east-1'
Paperclip::Attachment.default_options[:s3_protocol] = 'https'

我做错了什么?在使用paperclip的S3中,在Ruby中显示图像的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

解决了它。

删除了paperclip.rb初始化程序,并添加了:url =&gt; &#39;:s3_domain_url&#39;到我的个人资料模型