【问题标题】:Rails paperclip not showing imageRails回形针不显示图像
【发布时间】:2014-11-14 01:56:19
【问题描述】:

我是回形针的新手,我想看看它是如何工作的。我生成了一个简单的模型 Monkey 并得到以下内容:

rails g scaffold monkey description age:datetime
rails g paperclip monkey facepic
rake db:migrate

型号

class Monkey< ActiveRecord::Base

    has_attached_file :facepic, :styles => { :medium => "300x300>", :thumb => "100x100>" }

结束

查看新/编辑

<%= form_for @monkey, :url => monkies_path, :html => { :multipart => true } do |f| %>
 ...
  <div class="field">
    <%= f.label :facepic %><br>
    <%= f.file_field :facepic %>
  </div>

观看节目

 <%= image_tag @monkey.facepic.url %>

控制器

@monkey = Monkey.new(monkey_params)

我可以创建新猴子,但显示视图似乎找不到上传的文件。我没有错误消息,除了“missing.png”的路由错误。上传的图像没有任何痕迹。我正在使用 Rails 4.1.6。我在这里想念什么?我该如何解决这个问题?安装了 gem,还安装了 imagemagick。

日志是这样说的:

ActionController::RoutingError (No route matches [GET] "/facepics/original/missing.png"):
...

Started GET "/monkies/new" for 127.0.0.1 at 2014-09-19 14:40:22 +0200
Processing by MonkiesController#new as HTML
  Rendered monkies/_form.html.erb (4.0ms)
  Rendered monkies/new.html.erb within layouts/application (5.0ms)
Completed 500 Internal Server Error in 12ms

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"monkies"} missing required keys: [:id]):

但是当我创建一个新猴子时没有显示错误消息... :'(

编辑:

Monkey 模型已创建,但回形针列仍为空。

【问题讨论】:

  • 您是否将:facepic 添加到您的强参数中?
  • 您的公共文件夹中有图片吗?您可能会在您的公共文件夹中找到一个文件夹调用系统,并在其中找到上传的图像。
  • 公用文件夹中没有图像,我没有将它添加到强参数中!!!我现在就试试!
  • 现在我得到 Paperclip::Errors::MissingRequiredValidatorError with :facepic in strong params :S

标签: ruby-on-rails paperclip


【解决方案1】:

我很遗憾地说我的问题最终是一堆东西:

  1. 必须将 :facepic 添加到 strong 参数
  2. 必须在模型中添加内容类型验证
  3. Paperclip 有一个依赖项... file-命令,该命令未在 Windows 上提供。 可以在这里下载:http://sourceforge.net/projects/gnuwin32/?source=typ_redirect

毕竟这三个,它就像一个魅力!

谢谢大家!

【讨论】:

    【解决方案2】:

    此错误清楚地表明您的图像未保存,因为您的 has_attached_file 块中未指定路径和 url。应该是这样的:

    has_attached_file :facepic, 
      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
      :url => "/system/:attachment/:id/:style/:filename", 
      :styles => { :medium => "300x300>", :thumb => "100x100>" },
      :default_url => "path to default image"
    

    如果没有上传图片,这里 default_url 显示你想要的图片。更多详细信息,你可以去这里http://rdoc.info/gems/paperclip/4.2.0/Paperclip/ClassMethods%3ahas_attached_file

    对于其他错误,您可以点击此链接Paperclip::Errors::MissingRequiredValidatorError with Rails 4

    【讨论】:

      【解决方案3】:

      Paperclip version 4.0 开始,所有附件都必须包含content_type 验证file_name 验证,或明确声明它们'也不会有。

      如果您不执行任何操作,Paperclip 会引发 Paperclip::Errors::MissingRequiredValidatorError 错误。

      在您的情况下,您可以将以下任何一行添加到您的 Post 模型中,之后指定 has_attached_file :image

      选项 1:验证内容类型

      validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
      

      -或-另一种方式

      validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
      

      -或-另一种方式

      是使用 regex 来验证内容类型。

      选项 2:验证文件名

      validates_attachment_file_name :avatar, :matches => [/png\Z/, /jpe?g\Z/, /gif\Z/]
      

      选项 3:不验证

      如果出于某种疯狂原因(可能是有效,但我现在想不出一个),您不希望添加任何content_type 验证并允许人们欺骗 Content-Types 并在服务器上接收您不期望的数据,然后添加以下内容:

      do_not_validate_attachment_file_type :image
      

      注意:

      在上面的content_type/matches 选项中根据您的要求指定 MIME 类型。我刚刚提供了一些图像 MIME 类型供您开始使用。

      参考:

      如果您仍需要验证,请参阅 Paperclip: Security Validations。 :)

      详情可以去This question

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      • 2018-06-13
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多