【问题标题】:Rails - paperclip not uploading photos? shows missing.pngRails - 回形针不上传照片?显示缺失.png
【发布时间】:2013-11-09 00:57:37
【问题描述】:

我安装了 Paperclip gem,用它在我的模型中生成 :image 列。然后编辑视图让我上传照片:

<%= form_for @book = Book.new, :html => { :multipart => true } do |f| %>
...
...
...

<%= f.file_field :image %>

在我的模型中,我输入了这一行:

has_attached_file :image, :styles => { :smal => "200x200>"}
                  :url  => "/assets/books/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/books/:id/:style/:basename.:extension"

我可以选择上传照片,当我上传它时,它会报错为missing.png,即使图像名称不是这样,似乎根本没有上传照片。有什么问题? 编辑:完全错误:

    No route matches [GET] "/assets/books/17/small/thunderbird-logo-200x200.png"

 actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /usr/local/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /usr/local/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /usr/local/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.9ms)
  Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_route.html.erb (2.5ms)
  Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.3ms)
  Rendered /usr/local/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (27.2ms)

【问题讨论】:

  • 你能粘贴实际的图像吗?上传不会给你那个消息。 Missing.png 是使用回形针的模型不存在图像时默认使用的图像名称。听起来您还有其他验证失败或输出代码不正确
  • 好像是在上传图片,但是需要显示的时候却找不到上传的位置。。上传到/public/system/books/images/000/000/013/小的。我怎样才能告诉系统知道它上传该图像的位置?..
  • 它给出了这个错误:No route matches [GET] "/system/books/images/000/000/013/small/thunderbird-logo-200x200.png"

标签: ruby-on-rails upload ruby-on-rails-4 paperclip


【解决方案1】:

您可以通过添加控制器私有 service_params 来修复此错误

控制器:

  private
# Use callbacks to share common setup or constraints between actions.
def set_service
  @service = Service.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def service_params
  params.require(:service).permit(:title, :about, :price, :photo)
end

您必须将 :photo 添加到 :title、:about、:price (或您所拥有的东西)

【讨论】:

  • 添加新字段以将参数添加到控制器后非常常见的错误。谢谢!
【解决方案2】:

尝试显式设置路径和url

    has_attached_file :image,
      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
      :url => "/system/:attachment/:id/:style/:filename", 
      :styles => { :smal => "200x200>" }

【讨论】:

  • 我认为这与路由有关。即使我设置了整个路径,它仍然会引发路由错误。
  • 你能用你设置的路径和网址更新你的问题吗?
  • 更新问题。问题是如果我改变路径,它仍然可以正确上传照片,但我无法通过浏览器看到它......
  • 您是如何调用图像以将其显示在您的视图中的?
  • &lt;%= image_tag @book.image.url(:small) %&gt;
猜你喜欢
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多