سؤال

I am using the Paperclip gem with Rails to upload images, and when I use the img tag helper with the gem it outputs the wrong URL. Here is the model code:

class Org < ActiveRecord::Base
    has_many :event
    has_many :solookup
    belongs_to :student

    has_attached_file :org_pic, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/assets/clubhouse.jpg", :storage => :s3, :s3_credentials => Proc.new{|a| a.instance.s3_credentials}, :s3_host_name => "branchapp.s3.amazonaws.com"
    validates_attachment_content_type :org_pic, :content_type => /\Aimage\/.*\Z/

    def s3_credentials
        {:bucket => "branchapp", :access_key_id => "hidden", :secret_access_key => "hidden"}
    end
end

The upload works great, but the outputted url is like so:

http://branchapp.s3.amazonaws.com/branchapp/orgs/org_pics/000/000/002/original/IMG_0539.JPG?1396413590

I cannot figure out how to remove the /branchapp after the .com. If that is removed the link works without problem. How can I do this?

هل كانت مفيدة؟

المحلول

In has_attached_file you need to override the url option. By default the url uses ":s3_path_url" which puts the bucket in the url like you see. You need to use ":s3_domain_url" instead.

Add:

:url => ":s3_domain_url"

to your has_attached_file options.

Note: ":s3_domain_url" should prefix the host with the bucket name so you might need to remove branchapp from your s3_host_name option. (:s3_host_name => "s3.amazonaws.com")

نصائح أخرى

To give you some more input, you may wish to look at the :s3 documenation for Paperclip. We use this setup (which works great):

#config/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com" 

Paperclip::Attachment.default_options.merge!({
   storage: :s3,
   s3_host_name: 's3-eu-west-1.amazonaws.com',
   s3_credentials: {
      access_key_id: ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
   },
   bucket: ENV['S3_BUCKET_NAME']
})

This allows us to call:

#app/models/image.rb
Class Image < ActiveRecord::Base
    has_attached_file :image
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top