Reputation: 6897
I have a Rails 4 app, with a Post model, and I want to allow users to attach an image to an instance of it.
—————
UPDATE: here is my Post
table schema:
create_table "posts", force: :cascade do |t|
t.integer "calendar_id"
t.date "date"
t.time "time"
t.string "subject"
t.string "format"
t.text "copy"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
—————
I installed Paperclip and it was working, until I deployed to Heroku and had to fix things, as explained in this other Stack Overflow question.
Also, I updated my code to allow multiple image formats.
Here is my post.rb
file:
class Post < ActiveRecord::Base
belongs_to :calendar
has_many :comments
has_attached_file :image, styles: { small: "64x64", med: "100x100", large: "200x200" }
validates_attachment :image, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },
:size => { :in => 0..3000.kilobytes }
end
In my posts_controller.rb
, I have the following code:
def post_params
params.require(:post).permit(:date, :time, :subject, :format, :copy, :image => [:image_file_name, :image_file_size, :image_content_type, :image_updated_at])
end
Problem is, whenever I try to upload an image, I get no error message but the image is not displayed within the post it is attached to.
Instead, I get the missing.png
file:
When I check my server logs, I get the following:
Started GET "/calendars/3" for ::1 at 2015-10-06 16:19:32 -0700
Processing by CalendarsController#show as HTML
Parameters: {"id"=>"3"}
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = ? LIMIT 1 [["id", 3]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Calendar Load (0.1ms) SELECT "calendars".* FROM "calendars" INNER JOIN "administrations" ON "calendars"."id" = "administrations"."calendar_id" WHERE "administrations"."user_id" = ? AND "calendars"."id" = ? LIMIT 1 [["user_id", 1], ["id", 3]]
Post Exists (0.2ms) SELECT 1 AS one FROM "posts" WHERE "posts"."calendar_id" = ? LIMIT 1 [["calendar_id", 3]]
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."calendar_id" = ? [["calendar_id", 3]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 2]]
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? [["post_id", 3]]
Rendered calendars/show.html.erb within layouts/calendars (39.3ms)
Completed 200 OK in 117ms (Views: 91.0ms | ActiveRecord: 2.7ms)
Started GET "/assets/application.self-58d5a7d66aaf8b03404e0b4152b2fbea39b5e549a83c80b6294ee67cdefa38b4.css?body=1" for ::1 at 2015-10-06 16:19:33 -0700
Started GET "/assets/turbolinks.self-c37727e9bd6b2735da5c311aa83fead54ed0be6cc8bd9a65309e9c5abe2cbfff.js?body=1" for ::1 at 2015-10-06 16:19:33 -0700
Started GET "/assets/bootstrap/dropdown.self-30536ae4d54b2685c26b5787ed0eb549a9075717fe690cce6270873bedf2df00.js?body=1" for ::1 at 2015-10-06 16:19:33 -0700
Started GET "/assets/jquery_ujs.self-d456baa54c1fa6be2ec3711f0a72ddf7a5b2f34a6b4f515f33767d6207b7d4b3.js?body=1" for ::1 at 2015-10-06 16:19:33 -0700
Started GET "/assets/application.self-7c370d9536d7d0d6a0f7cd7f9826692acd93e4fb05ba46f7b630b879740343d3.js?body=1" for ::1 at 2015-10-06 16:19:33 -0700
Started GET "/assets/jquery.self-a714331225dda820228db323939889f149aec0127aeb06255646b616ba1ca419.js?body=1" for ::1 at 2015-10-06 16:19:33 -0700
—————
UPDATE 2: as recommended in this other Stack Overflow question, I also tried to add
:url => "/system/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension"
to my post.rb
file but still run into the same issue.
—————
UPDATE 3: I thought I had a problem of image persistence, because of Heroku ephemeral filesystem (which I was probably having, actually) so I followed this Heroku tutorial and setup an AWS S3 bucket to store the uploaded images from my app.
However, this did not seem to fix the problem, I still get the same missing.png image when I upload a file.
Here are my new Heroku logs::
2015-10-07T18:24:20.167094+00:00 app[web.1]: Completed 302 Found in 10ms (ActiveRecord: 2.4ms)
2015-10-07T18:24:20.284296+00:00 app[web.1]: Processing by CalendarsController#show as HTML
2015-10-07T18:24:20.159347+00:00 app[web.1]: Post Load (1.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 2]]
2015-10-07T18:24:20.157141+00:00 app[web.1]: Processing by PostsController#update as HTML
2015-10-07T18:24:20.286138+00:00 app[web.1]: Calendar Load (0.8ms) SELECT "calendars".* FROM "calendars" WHERE "calendars"."id" = $1 LIMIT 1 [["id", 1]]
2015-10-07T18:24:20.284320+00:00 app[web.1]: Parameters: {"id"=>"1"}
2015-10-07T18:24:20.291689+00:00 app[web.1]: Calendar Load (0.8ms) SELECT "calendars".* FROM "calendars" INNER JOIN "administrations" ON "calendars"."id" = "administrations"."calendar_id" WHERE "administrations"."user_id" = $1 AND "calendars"."id" = $2 LIMIT 1 [["user_id", 1], ["id", 1]]
2015-10-07T18:24:20.396896+00:00 app[web.1]: Comment Load (1.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = $1 [["post_id", 2]]
2015-10-07T18:24:20.301725+00:00 app[web.1]: Post Load (0.9ms) SELECT "posts".* FROM "posts" WHERE "posts"."calendar_id" = $1 [["calendar_id", 1]]
2015-10-07T18:24:20.407837+00:00 app[web.1]: Rendered calendars/show.html.erb within layouts/calendars (115.0ms)
2015-10-07T18:24:20.569606+00:00 app[web.1]: Started GET "/images/med/missing.png" for 24.205.62.204 at 2015-10-07 18:24:20 +0000
2015-10-07T18:24:20.409206+00:00 app[web.1]: Completed 200 OK in 125ms (Views: 110.2ms | ActiveRecord: 9.5ms)
2015-10-07T18:24:20.571583+00:00 app[web.1]:
2015-10-07T18:24:20.571586+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/med/missing.png"):
2015-10-07T18:24:20.571588+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-10-07T18:24:20.571590+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-10-07T18:24:20.571591+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:38:in `call_app'
2015-10-07T18:24:20.571592+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `block in call'
2015-10-07T18:24:20.571593+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-10-07T18:24:20.571594+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-10-07T18:24:20.571595+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-10-07T18:24:20.571597+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `call'
2015-10-07T18:24:20.571599+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-10-07T18:24:20.571598+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-10-07T18:24:20.571601+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-10-07T18:24:20.571602+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-10-07T18:24:20.571603+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-10-07T18:24:20.571604+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-10-07T18:24:20.571606+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-10-07T18:24:20.571607+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/engine.rb:518:in `call'
2015-10-07T18:24:20.571609+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/configuration.rb:78:in `call'
2015-10-07T18:24:20.571610+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:541:in `handle_request'
2015-10-07T18:24:20.571611+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:388:in `process_client'
2015-10-07T18:24:20.571612+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:270:in `block in run'
2015-10-07T18:24:20.571608+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/application.rb:164:in `call'
2015-10-07T18:24:20.571614+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `call'
2015-10-07T18:24:20.571615+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
2015-10-07T18:24:20.571616+00:00 app[web.1]:
2015-10-07T18:24:20.571617+00:00 app[web.1]:
2015-10-07T18:24:20.408512+00:00 heroku[router]: at=info method=GET path="/calendars/1" host=calendy.herokuapp.com request_id=b12e0fb5-e59e-4593-8b0e-bd0031ffdf43 fwd="24.205.62.204" dyno=web.1 connect=2ms service=132ms status=200 bytes=11692
2015-10-07T18:24:20.569758+00:00 heroku[router]: at=info method=GET path="/images/med/missing.png" host=calendy.herokuapp.com request_id=0a463145-5d01-4ebb-b752-17a75b878c45 fwd="24.205.62.204" dyno=web.1 connect=1ms service=5ms status=404 bytes=1789
2015-10-07T18:24:20.564372+00:00 heroku[router]: at=info method=GET path="/assets/application-a35d66e0cb25af3843317168db09bdc3b1255dd810c9f82819a82471ddddf03c.css" host=calendy.herokuapp.com request_id=13404b31-cc7b-4208-a9c0-af3ba71527b2 fwd="24.205.62.204" dyno=web.1 connect=2ms service=2ms status=304 bytes=93
2015-10-07T18:24:21.289983+00:00 heroku[router]: at=info method=GET path="/assets/font-awesome.css" host=calendy.herokuapp.com request_id=655b72fe-2ece-4006-9a27-46d0b56a52a6 fwd="24.205.62.204" dyno=web.1 connect=612ms service=6ms status=404 bytes=1789
2015-10-07T18:24:21.285564+00:00 app[web.1]: Started GET "/assets/font-awesome.css" for 24.205.62.204 at 2015-10-07 18:24:21 +0000
2015-10-07T18:24:21.440341+00:00 app[web.1]:
2015-10-07T18:24:21.440344+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/images/med/missing.png"):
2015-10-07T18:24:21.440350+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `block in call'
2015-10-07T18:24:21.440346+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-10-07T18:24:21.440348+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-10-07T18:24:21.440349+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:38:in `call_app'
2015-10-07T18:24:21.440351+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-10-07T18:24:21.440352+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-10-07T18:24:21.440354+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-10-07T18:24:21.440355+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `call'
2015-10-07T18:24:21.440356+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-10-07T18:24:21.440359+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-10-07T18:24:21.440357+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-10-07T18:24:21.440360+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-10-07T18:24:21.440361+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-10-07T18:24:21.440362+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-10-07T18:24:21.440363+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-10-07T18:24:21.440365+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/engine.rb:518:in `call'
2015-10-07T18:24:21.440366+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/application.rb:164:in `call'
2015-10-07T18:24:21.440367+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/configuration.rb:78:in `call'
2015-10-07T18:24:21.440368+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:541:in `handle_request'
2015-10-07T18:24:21.440369+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:388:in `process_client'
2015-10-07T18:24:21.440370+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:270:in `block in run'
2015-10-07T18:24:21.440372+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
2015-10-07T18:24:21.440371+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `call'
2015-10-07T18:24:21.440373+00:00 app[web.1]:
2015-10-07T18:24:21.440374+00:00 app[web.1]:
2015-10-07T18:24:21.287683+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-10-07T18:24:21.287676+00:00 app[web.1]:
2015-10-07T18:24:21.287684+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:38:in `call_app'
2015-10-07T18:24:21.287685+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `block in call'
2015-10-07T18:24:21.287680+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/assets/font-awesome.css"):
2015-10-07T18:24:21.287682+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
2015-10-07T18:24:21.287711+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-10-07T18:24:21.287687+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-10-07T18:24:21.287688+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-10-07T18:24:21.287689+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-10-07T18:24:21.287710+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/rack/logger.rb:20:in `call'
2015-10-07T18:24:21.287719+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/ssl.rb:24:in `call'
2015-10-07T18:24:21.287713+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
2015-10-07T18:24:21.287716+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack-4.2.2/lib/action_dispatch/middleware/static.rb:113:in `call'
2015-10-07T18:24:21.287714+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
2015-10-07T18:24:21.287715+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport-4.2.2/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
2015-10-07T18:24:21.287717+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2015-10-07T18:24:21.287728+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:541:in `handle_request'
2015-10-07T18:24:21.287730+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:270:in `block in run'
2015-10-07T18:24:21.287720+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/engine.rb:518:in `call'
2015-10-07T18:24:21.287732+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
2015-10-07T18:24:21.287729+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/server.rb:388:in `process_client'
2015-10-07T18:24:21.287721+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/railties-4.2.2/lib/rails/application.rb:164:in `call'
2015-10-07T18:24:21.287731+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/thread_pool.rb:106:in `call'
2015-10-07T18:24:21.287727+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/puma-2.13.4/lib/puma/configuration.rb:78:in `call'
2015-10-07T18:24:21.287733+00:00 app[web.1]:
2015-10-07T18:24:21.287734+00:00 app[web.1]:
2015-10-07T18:24:21.438516+00:00 app[web.1]: Started GET "/images/med/missing.png" for 24.205.62.204 at 2015-10-07 18:24:21 +0000
2015-10-07T18:24:21.442123+00:00 heroku[router]: at=info method=GET path="/images/med/missing.png" host=calendy.herokuapp.com request_id=584508b8-77fe-481c-9f9c-90e78868d9b5 fwd="24.205.62.204" dyno=web.1 connect=1ms service=5ms status=404 bytes=1789
2015-10-07T18:24:21.647204+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=calendy.herokuapp.com request_id=619a70fc-aaa8-4615-bf82-c22478645662 fwd="24.205.62.204" dyno=web.1 connect=1ms service=2ms status=304 bytes=93
—————
I am obviously doing something wrong / missing something: any idea of what it is and how I can fix it?
Upvotes: 0
Views: 396
Reputation: 93
Please check the file path for your default image. You can also attempt to put the default image in your public folder rather than in your assets file path for images. Please be sure to rake db:precompile assets when you deploy to Heroku.
Upvotes: 1