【问题标题】:How to specify :path and :url to save img in Paperclip, Rails 3.0.12如何指定 :path 和 :url 以在 Paperclip,Rails 3.0.12 中保存图像
【发布时间】:2012-04-12 06:43:40
【问题描述】:

我在 Rails 3.0 应用程序中使用 Paperclip,以便向用户添加头像,但由于路径已关闭,它不会保存图像。这是我得到的:

在 2012-04-11 23:38:29 -0700 开始 GET "/profilepics/small/missing.png" for 127.0.0.1

ActionController::RoutingError(没有路由匹配“/profilepics/small/missing.png”):

我的用户模型有:

    has_attached_file :profilepic, :styles => { :small => "150x150>" }

我应该为 :path => & :url => 放什么?

表格如下:

      <% form_for @user, :html => { :multipart => true } do |f| %>

      <%= f.file_field :profilepic %>

      <% end %>

日志看起来像:

在 2012-04-12 00:33:51 -0700 开始为 127.0.0.1 获取“/system/profilepics/small/missing.png”

ActionController::RoutingError(没有路由匹配“/system/profilepics/small/missing.png”):

在救援/布局中渲染 /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb (1.2ms)

【问题讨论】:

  • 你不一定需要指定:path或:url,默认图片保存到:rails_root/public/system/:attachment/:id/:style/:filename
  • 我只是在上传时不断收到损坏的 img...:(
  • 能否也添加您的视图代码?
  • 当然,视图是

标签: ruby-on-rails


【解决方案1】:

查看我的示例:

   has_attached_file :avatar, :styles => { :thumb => "50x50#", :large => "1000x1000>", :medium => "200x200#" },
     :default_url => "/system/avatars/:style/missing.png",
     :url  => "/system/:attachment/:id/:style_:filename",
     :path => ":rails_root/public/system/:attachment/:id/:style_:filename"
  • :default_url - 用户未上传头像时默认图片的路径
  • “#” - 此符号用于裁剪图像

然后您可以这样显示您的图像:

<%=image_tag(@user.avatar.url(:thumb))%>
<%=image_tag(@user.avatar.url(:medium))%>

【讨论】:

  • 嗯,所以我想我的问题是为什么我上传后显示missing.png?
  • 您的表单不应该是
  • 在您发布表单后检查日志以了解发生了什么
  • 是的,它似乎每次都显示默认图像,这意味着它没有保存:(
  • 正在寻找 :default_url 的东西。谢谢!
【解决方案2】:

现在工作!!!

对于在同一问题上苦苦挣扎的人,这里有一些重要的事情要始终确保并检查:

  1. 在您的表单中,始终指定 { :multipart => true } 否则,表单将不接受文件附件。

    <%= form_for @user, :html => **{ :multipart => true }** do |f| %>
    
  2. 在您的 user.rb(或您要添加附件的任何模型)中,制作 attr_accessible :photo(或您为附件命名的任何名称)

  3. 安装新的 Gem 后,请务必重新启动服务器。

:) 谢谢大家!!!!

【讨论】:

    【解决方案3】:

    如果您只是想显示图像,则无需提供 url 和路径选项。

    在显示页面中使用此行,它将显示图像...

         <%=image_tag(@user.profilepic.url(:small))%>
    

    然后享受......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多