Rails 3 Paperclip 和 S3 的路由错误

发布于 2024-10-23 15:08:50 字数 441 浏览 3 评论 0原文

我完全按照本教程进行操作并收到了尝试将 JPG 上传到我的本地环境后出现以下错误(我还没有推送到 Heroku):

ActionController::RoutingError(否 路线匹配 “/logos/medium/missing.png”)

我检查了类似的教程,但没有一个提到需要远程图像的路由。就像我说的,我已经仔细检查了本教程,并且正在执行它要求我做的所有事情,那么我在这里可能会遗漏哪些步骤呢?唯一的区别是我已经在模型中将“照片”列指定为与 has_attached_file 变量同名的二进制数据类型......这可能会导致冲突吗?

I followed this tutorial exactly and received the following error after trying to upload a JPG to my local environment (I have not pushed to Heroku yet):

ActionController::RoutingError (No
route matches
"/logos/medium/missing.png")

I checked similar tutorials and none of them mentioned requiring routes for your remote images. Like I said, I have triple-checked this tutorial and I'm doing everything it asks of me, so what steps could I be missing here? The ONLY difference is that I already had specified a "photo" column in my model as a binary datatype with the same name as the has_attached_file variable...could that be causing the conflict?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆夏时光 2024-10-30 15:08:50

当我意识到对其他人来说了解问题所在时很有价值时,我正打算删除此内容。

因此,如果您遵循该教程,您将会做得很好。但是,如果您使用 attr_accessible 来保护模型的属性,则需要在模型中指定所有属性,以使这些图像实际保存到您的 S3 存储桶中。

如果您在日志中看到以下内容,则会出现提示:

警告:无法批量分配受保护的属性:照片

这可以让您知道数据库中有需要通过以下方式公开的受保护列: attr_accessible

如果您按照我在问题中提供的示例教程进行操作,代码将如下所示:

class Product < ActiveRecord::Base
  attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
  belongs_to :page
  has_attached_file :photo,
    :styles =>{
      :thumb  => "100x100",
      :medium => "200x200",
      :large => "600x400"
    },
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => ":attachment/:id/:style.:extension",
    :bucket => 'yourbucket'
end

I was about to delete this when I realized it will be valuable for others to learn what went wrong.

So if you follow that tutorial to the tee you will do just fine. HOWEVER, if you use attr_accessible to protect the attributes of your model, you need to specify ALL of them in your model in order to make these images actually save to your S3 bucket.

A hint will be if you see the following in your logs:

WARNING: Can't mass-assign protected attributes: photo

This lets you know that you have protected columns in your database that need to be exposed through attr_accessible

Here's what the code would look like if you were to follow the example tutorial I provided in my question:

class Product < ActiveRecord::Base
  attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
  belongs_to :page
  has_attached_file :photo,
    :styles =>{
      :thumb  => "100x100",
      :medium => "200x200",
      :large => "600x400"
    },
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => ":attachment/:id/:style.:extension",
    :bucket => 'yourbucket'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文