Paper Clip不保存缩略图

时间:2015-09-30 04:42:28

标签: ruby-on-rails-4 paperclip

我在模型中有以下代码:

   has_attached_file :avatar, 
           path: ':class/:attachment/:id/:style/:basename.:extension', 
           :styles=> { :original => "500x500",
                      :small => "200x200"
                   }, default_url: "icons/user.png"

我还安装了imagemagick。

which convert
/usr/bin/convert

在development.rb

 Paperclip.options[:command_path] = %w(/usr/local/bin/ /usr/bin/)

我也尝试了以下内容:

   Paperclip.options[:command_path] = "/usr/bin/"

但是当我尝试上传图片时,它只会保存原始文件。来自日志的消息是:

[paperclip] saving user_profiles/avatars/53/original/2015-06-08-153213.jpg

我缺少什么?

1 个答案:

答案 0 :(得分:0)

在您的模型中,您可以添加以下代码。不需要担心代码中指定的大小,您可以将其更改为您需要的大小。

  has_attached_file :avatar, styles: { medium: '200x200>', thumb: '80x80>' }, default_url: "/icons/user.png"
  validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/

在您的情况下,转换路径是/ usr / bin / convert。这是默认路径。因此,您无需在development.rb文件中指定此项。你能否删除它并尝试一下。

还要确保你的控制器允许使用params。

示例代码

# In Controller File
private
def user_params
  params.require(:user).permit(:email, :avatar)
end
相关问题