ruby-on-rails 我如何修复错误错误的URI(不是URI?):零

o2g1uqev  于 2022-12-15  发布在  Ruby
关注(0)|答案(3)|浏览(128)

跑步

Rails 6.0.2.1
Ruby 2.6.5

我正在使用ActiveStorage和DropZoneJS实现照片上传,但在这一点上,它在这个特定的页面/users/2上抛出了一个错误。
更好的错误显示了这一点

URI::InvalidURIError at /users/2
bad URI(is not URI?): nil

车型

def cover_photo(size_x, size_y)
        if self.photos.length > 0
          self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed.service_url
        else
          "blank.jpg"
        end
      end
    end

我的日志文件

Started GET "/users/2" for ::1 at 2020-05-08 11:39:09 +0000
Processing by UsersController#show as HTML
  Parameters: {"id"=>"2"}
  [1m[36mUser Load (0.1ms)[0m  [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?[0m  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:3:in `show'
  Rendering users/show.html.erb within layouts/application
  [1m[36mCar Load (0.1ms)[0m  [1m[34mSELECT "cars".* FROM "cars" WHERE "cars"."user_id" = ?[0m  [["user_id", 2]]
  ↳ app/views/users/show.html.erb:29
  [1m[36mActiveStorage::Attachment Load (0.1ms)[0m  [1m[34mSELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ?[0m  [["record_id", 1], ["record_type", "Car"], ["name", "photos"]]
  ↳ app/models/car.rb:14:in `cover_photo'
  [1m[36mActiveStorage::Blob Load (0.1ms)[0m  [1m[34mSELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?[0m  [["id", 26], ["LIMIT", 1]]
  ↳ app/models/car.rb:15:in `cover_photo'
[36m  Disk Storage (0.0ms) [0m[34mChecked if file exists at key: variants/4u0kx27vmugn57zwn96o2t27mr71/36e628c6ec62cc8383a3ee5c0c8433e912780efead13846813a9f63693dd17eb (yes)[0m
[36m  Disk Storage (0.5ms) [0m[34mGenerated URL for file at key: variants/4u0kx27vmugn57zwn96o2t27mr71/36e628c6ec62cc8383a3ee5c0c8433e912780efead13846813a9f63693dd17eb ()[0m
  Rendered users/show.html.erb within layouts/application (Duration: 18.4ms | Allocations: 4342)
Completed 500 Internal Server Error in 21ms (ActiveRecord: 0.4ms | Allocations: 5357)


URI::InvalidURIError - bad URI(is not URI?): nil:
  app/models/car.rb:15:in `cover_photo'
  app/views/users/show.html.erb:36
  app/views/users/show.html.erb:32

Started POST "/__better_errors/f90fad0cb966afba/variables" for ::1 at 2020-05-08 11:39:09 +0000

我该如何解决这个问题?cover_photo有什么问题?

z31licg0

z31licg01#

好了我把.service_url
self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed.service_url
所以就变成了
self.photos[0].variant(resize_to_limit: [size_x, size_y]).processed
现在错误消失了,页面加载完美。不知道为什么
顺便说一句,有人能解释吗?

vwhgwdsa

vwhgwdsa2#

我在rails控制台中遇到了同样的问题,解决方案运行了一次:

ActiveStorage::Current.host = 'http://localhost:3000'

我从@Jan的回答和here中获得了此信息。

von4xj4u

von4xj4u3#

根据您的设置,我已经通过在使用variant之前定义ActiveStorage::Current.host = "http://localhost:3000"解决了这个问题
https://github.com/rails/rails/issues/40855

相关问题