我有一个带有Post模型的Rails 4应用程序,我希望允许用户将图像附加到它的实例。
- - - -
更新:这是我的Post
表架构:
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
- - - -
我安装了Paperclip并且它正在工作,直到我部署到Heroku并且必须解决问题,如this other Stack Overflow question中所述。
另外,我更新了我的代码以允许多种图像格式。
这是我的post.rb
文件:
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
在我的posts_controller.rb
中,我有以下代码:
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
问题是,每当我尝试上传图片时,都没有收到任何错误消息,但图片不会显示在附加的帖子中。
相反,我得到了missing.png
文件:
当我检查我的服务器日志时,我得到以下内容:
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
- - - -
更新2 :根据this other Stack Overflow question的建议,我也尝试添加
:url => "/system/:class/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension"
到我的post.rb
文件,但仍遇到同样的问题。
- - - -
更新3 :我认为我有图像持久性问题,因为Heroku短暂文件系统(实际上我可能已经拥有)所以我跟着this Heroku tutorial并设置了AWS S3存储来自我的应用程序的上传图像。
然而,这似乎没有解决问题,我上传文件时仍然会得到相同的missing.png图像。
以下是我的新Heroku日志::
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
- - - -
我显然做错了什么/遗失了什么:知道它是什么以及如何解决它?
答案 0 :(得分:1)
请检查默认图片的文件路径。您还可以尝试将默认图像放在公用文件夹中,而不是放在图像的资源文件路径中。部署到Heroku时,请务必使用rake db:precompile资产。