简体   繁体   English

如何指定:path和:url在Paperclip,Rails 3.0.12中保存img

[英]How to specify :path and :url to save img in Paperclip, Rails 3.0.12

I'm using Paperclip with my Rails 3.0 app in order to add an avatar to a user, and it won't save the image because the paths are off. 我正在将Paperclip与我的Rails 3.0应用程序一起使用以向用户添加头像,并且由于路径已关闭,因此无法保存图像。 Heres what I get: 这是我得到的:

Started GET "/profilepics/small/missing.png" for 127.0.0.1 at 2012-04-11 23:38:29 -0700 在2012-04-11 23:38:29 -0700开始获取127.0.0.1的“/profilepics/small/missing.png”

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

My user model has: 我的用户模型有:

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

What should I put for :path => & :url => ? 我该怎么做:path =>&:url =>?

Form looks like : 表格如下:

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

      <%= f.file_field :profilepic %>

      <% end %>

Logs look like : 日志看起来像:

Started GET "/system/profilepics/small/missing.png" for 127.0.0.1 at 2012-04-12 00:33:51 -0700 在2012-04-12 00:33:51 -0700开始获取127.0.0.1的“/system/profilepics/small/missing.png”

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

Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms) 在救援/布局(1.2ms)内呈现/usr/lib/ruby/gems/1.9.1/gems/actionpack-3.0.12/lib/action_dispatch/middleware/templates/rescues/routing_error.erb

See my sample: 看我的样本:

   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 - is the path for the default image, when the user didn't upload any avatar :default_url - 当用户未上传任何头像时,是默认图像的路径
  • "#" - this symbol is to crop image “#” - 此符号用于裁剪图像

then you can show your images as such: 然后你可以这样显示你的图像:

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

Works now!!! 现在工作!!!

For people struggling with the same issue, here's a few important things to ALWAYS make sure and check: 对于那些在同一问题上苦苦挣扎的人来说,这里有一些重要的事情要始终确认并检查:

  1. In your form, always specify { :multipart => true } otherwise, the form won't accept file attachments. 在您的表单中,始终指定{:multipart => true}否则,表单将不接受文件附件。

     <%= form_for @user, :html => **{ :multipart => true }** do |f| %> 
  2. In your user.rb (or whatever model you want to add attachments to ), make attr_accessible :photo (or whatever you name your attachment) 在您的user.rb(或任何您想要添加附件的模型)中,制作attr_accessible:photo (或您为附件命名的任何内容)

  3. ALWAYS restart your server after installing a new Gem. 安装新Gem后,务必重新启动服务器。

:) Thanks guys!!!! :) 多谢你们!!!!

There is no need to given url and path option if you simply want to display an image. 如果您只是想显示图像,则无需提供URL和路径选项。

Use this line in show page and it will display image... 在显示页面中使用此行,它将显示图像...

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

And enjoy.............. 享受..............

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM