简体   繁体   中英

Issues running paperclip on heroku using ruby on rails

I am using paperclip for some image uploading in my ruby on rails application. Everything works fine locally.

When I deploy to heroku, the upload appears to work but when I look at my item it is obvious the upload has somehow failed - see Companylogo field:

https://whats-on-today.herokuapp.com/events/2

Please see the form and try it yourself:

https://whats-on-today.herokuapp.com/events/new

This is my gemfile:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.8'
# Use sqlite3 as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

#add bootstrap css
gem "twitter-bootstrap-rails"
gem "therubyracer"
gem "less-rails"

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

#Add paperclip for image uploading - requires imagemagick - need rmagick for heroku?
gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
gem 'rmagick'

gem "rake", '10.4.2'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

And these are the heroku logs - where I see no problems....

2015-01-25T22:01:35.176977+00:00 heroku[web.1]: Process exited with status 143
2015-01-25T22:01:38.410781+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 14913 -e production`
2015-01-25T22:01:43.197130+00:00 app[web.1]: [2015-01-25 22:01:43] INFO  ruby 2.0.0 (2014-11-13) [x86_64-linux]
2015-01-25T22:01:43.197109+00:00 app[web.1]: [2015-01-25 22:01:43] INFO  WEBrick 1.3.1
2015-01-25T22:01:43.197447+00:00 app[web.1]: [2015-01-25 22:01:43] INFO  WEBrick::HTTPServer#start: pid=3 port=14913
2015-01-25T22:01:43.641141+00:00 heroku[web.1]: State changed from starting to up
2015-01-25T22:01:45.345186+00:00 heroku[router]: at=info method=GET path="/events/events" host=whats-on-today.herokuapp.com request_id=3560f69e-1093-4993-a17c-5ce8d2ee8eb3 fwd="90.202.95.185" dyno=web.1 connect=2ms service=124ms status=404 bytes=1829
2015-01-25T22:01:45.584826+00:00 heroku[router]: at=info method=GET path="/events/events" host=whats-on-today.herokuapp.com request_id=a74abcc9-998e-4ed1-abbf-5770154ee91e fwd="90.202.95.185" dyno=web.1 connect=4ms service=15ms status=404 bytes=1829
2015-01-25T22:01:45.941976+00:00 heroku[router]: at=info method=GET path="/events" host=whats-on-today.herokuapp.com request_id=e492071a-85d9-4abc-a563-d7f47fa5059d fwd="90.202.95.185" dyno=web.1 connect=1ms service=72ms status=200 bytes=5296
2015-01-25T22:01:46.155824+00:00 heroku[router]: at=info method=GET path="/assets/application-2b56caae7bd0b485593c1fca2397cc26.css" host=whats-on-today.herokuapp.com request_id=717efe34-3d3d-4bcd-81cd-8f6ed3c4c68e fwd="90.202.95.185" dyno=web.1 connect=2ms service=86ms status=304 bytes=231
2015-01-25T22:01:46.187234+00:00 heroku[router]: at=info method=GET path="/assets/application-09164813eb76f663d73f61fbdd0dbbea.js" host=whats-on-today.herokuapp.com request_id=c2462f3b-728b-4a00-8675-699a14c57648 fwd="90.202.95.185" dyno=web.1 connect=0ms service=113ms status=304 bytes=231
2015-01-25T22:01:48.523726+00:00 heroku[router]: at=info method=GET path="/events/new" host=whats-on-today.herokuapp.com request_id=7179c102-4baa-4db4-b96b-5187ce63edf2 fwd="90.202.95.185" dyno=web.1 connect=3ms service=122ms status=200 bytes=12000
2015-01-25T22:02:08.602578+00:00 heroku[router]: at=info method=POST path="/events" host=whats-on-today.herokuapp.com request_id=cfa83d61-6335-4f69-aba6-300226da8d46 fwd="90.202.95.185" dyno=web.1 connect=1ms service=2146ms status=302 bytes=1057
2015-01-25T22:02:08.801389+00:00 heroku[router]: at=info method=GET path="/events/2" host=whats-on-today.herokuapp.com request_id=3b54580d-bbcf-44a2-9c4c-60a951f46917 fwd="90.202.95.185" dyno=web.1 connect=0ms service=90ms status=200 bytes=5557
2015-01-25T22:02:08.961458+00:00 heroku[router]: at=info method=GET path="/system/events/logos/000/000/002/original/.jpg?1422223328" host=whats-on-today.herokuapp.com request_id=99bad1d0-e9ae-4be3-8e4c-a2e4e2a13270 fwd="90.202.95.185" dyno=web.1 connect=1ms service=6ms status=404 bytes=1829
2015-01-25T22:02:08.954988+00:00 heroku[router]: at=info method=GET path="/assets/application-2b56caae7bd0b485593c1fca2397cc26.css" host=whats-on-today.herokuapp.com request_id=041af115-97ff-4d89-9e39-eb612cff5a72 fwd="90.202.95.185" dyno=web.1 connect=1ms service=7ms status=304 bytes=231
2015-01-25T22:02:09.068322+00:00 heroku[router]: at=info method=GET path="/assets/application-09164813eb76f663d73f61fbdd0dbbea.js" host=whats-on-today.herokuapp.com request_id=4c838874-33a1-4a0c-a823-b8e50af57d03 fwd="90.202.95.185" dyno=web.1 connect=1ms service=6ms status=304 bytes=231
2015-01-25T22:02:09.203374+00:00 heroku[router]: at=info method=GET path="/system/events/logos/000/000/002/original/.jpg?1422223328" host=whats-on-today.herokuapp.com request_id=4e30ba37-7545-47f7-9575-e02200fec206 fwd="90.202.95.185" dyno=web.1 connect=2ms service=8ms status=404 bytes=1829
2015-01-25T22:02:52.834418+00:00 heroku[router]: at=info method=GET path="/assets/application-09164813eb76f663d73f61fbdd0dbbea.js" host=whats-on-today.herokuapp.com request_id=f7f85d8c-d872-40ce-8068-33bf57167f67 fwd="90.202.95.185" dyno=web.1 connect=1ms service=6ms status=304 bytes=231
2015-01-25T22:02:52.688561+00:00 heroku[router]: at=info method=GET path="/events/" host=whats-on-today.herokuapp.com request_id=dbb5d10e-cea9-4add-89e7-5a0cca937cbb fwd="90.202.95.185" dyno=web.1 connect=2ms service=87ms status=200 bytes=5909
2015-01-25T22:02:52.837142+00:00 heroku[router]: at=info method=GET path="/assets/application-2b56caae7bd0b485593c1fca2397cc26.css" host=whats-on-today.herokuapp.com request_id=e39b298e-30de-42b6-a5de-00f97219f897 fwd="90.202.95.185" dyno=web.1 connect=1ms service=10ms status=304 bytes=231
2015-01-25T22:06:04.579739+00:00 heroku[router]: at=info method=GET path="/events/2" host=whats-on-today.herokuapp.com request_id=efa58555-c49c-4ea6-8f9c-970bc3672f12 fwd="90.202.95.185" dyno=web.1 connect=1ms service=40ms status=200 bytes=5135
2015-01-25T22:06:04.722708+00:00 heroku[router]: at=info method=GET path="/system/events/logos/000/000/002/original/.jpg?1422223328" host=whats-on-today.herokuapp.com request_id=d904339d-41b9-421f-889c-b19581517a16 fwd="90.202.95.185" dyno=web.1 connect=2ms service=6ms status=404 bytes=1829
2015-01-25T22:06:04.708682+00:00 heroku[router]: at=info method=GET path="/assets/application-2b56caae7bd0b485593c1fca2397cc26.css" host=whats-on-today.herokuapp.com request_id=926572f0-d5e6-4e95-998e-708581cb5563 fwd="90.202.95.185" dyno=web.1 connect=1ms service=5ms status=304 bytes=231
2015-01-25T22:06:04.717214+00:00 heroku[router]: at=info method=GET path="/assets/application-09164813eb76f663d73f61fbdd0dbbea.js" host=whats-on-today.herokuapp.com request_id=77373e30-4f30-4b30-b4fb-699682023e9a fwd="90.202.95.185" dyno=web.1 connect=1ms service=9ms status=304 bytes=231
2015-01-25T22:06:29.160729+00:00 heroku[router]: at=info method=GET path="/events/new" host=whats-on-today.herokuapp.com request_id=095b1c4f-b127-45f1-966a-d0e2e106ee41 fwd="90.202.95.185" dyno=web.1 connect=1ms service=93ms status=200 bytes=12000
2015-01-25T22:06:29.321758+00:00 heroku[router]: at=info method=GET path="/assets/application-2b56caae7bd0b485593c1fca2397cc26.css" host=whats-on-today.herokuapp.com request_id=cd6acd5d-9a95-401f-9599-b33d7c9b9ac3 fwd="90.202.95.185" dyno=web.1 connect=3ms service=7ms status=304 bytes=231
2015-01-25T22:06:29.350081+00:00 heroku[router]: at=info method=GET path="/assets/application-09164813eb76f663d73f61fbdd0dbbea.js" host=whats-on-today.herokuapp.com request_id=3dd3a871-2a7a-467c-9aaa-3929e551e74a fwd="90.202.95.185" dyno=web.1 connect=3ms service=4ms status=304 bytes=231

I believe I have Paperclip in file storage mode. Perhaps this is not allowed on heroku....if this is the case - how do I get Paperclip to store files in my pg database instead?

As i believe you cannot write directly to Heroku which is what i believe to be your problem. Therefore you have to use a file storage system-I have always used Amazon s3 for image storing. This will take a few amendments but it is fairly easily set up! Your first step will be to get an Amazon s3 account which is free-at least for the first year. You will need to add these to the gemfile

gem 'aws-sdk'

Following this you need to create a bucket on the Amazon s3 website which is what will act as an object container. Then you will need to get an access key and secret access key which you need to keep somewhere safe! In the config/production you will need to set something along the lines of this..

  config.paperclip_defaults = {
            :storage => :s3,
            :s3_credentials => {
              :bucket => ENV['AWS_BUCKET'],
              :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
              :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
            },
            :path => ":class/:id/:basename_:style.:extension",
            :url => ":s3_sg_url"
      }

Then in the config initializer a file called paperclip.rb needs to be made and something along the lines of the following should be input!

Paperclip.interpolates(:s3_sg_url) do |att, style| 
"#{att.s3_protocol}://s3-ap-southeast-1.amazonaws.com/#{att.bucket_name}/#{att.path(style)}"
end

For more information and instructions i suggest reading this https://devcenter.heroku.com/articles/paperclip-s3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM