我使用this tutorial但无法使用paperclip上传图片,它会显示“Avatar的扩展名与其内容不匹配”错误。
我确信ImageMagick已安装并正常运行。
形式:
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :avatar %><br>
<%= f.file_field :avatar %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
user_controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:avatar)
end
end
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
答案 0 :(得分:0)
创建方法
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html {
render :json => [@user.to_jq_user].to_json,
:content_type => 'text/html',
:layout => false
}
format.json { render json: {files: [@user.to_jq_user]}, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
user.rb
class Upload < ActiveRecord::Base
attr_accessible :user
has_attached_file :user
validates_attachment_content_type :user, :content_type => ["image/jpg", "image/jpeg", "image/png"]
include Rails.application.routes.url_helpers
def to_jq_user
{
"name" => read_attribute(:user_file_name),
"size" => read_attribute(:user_file_size),
"url" => user.url(:original),
"delete_url" => user_path(self),
"delete_type" => "DELETE"
}
end
end
答案 1 :(得分:0)
我认为这里可能存在一些问题。首先,我认为您的“Avatar有一个与其内容不匹配的扩展名”错误可能来自在Windows上运行而未安装file
。通过使用this answer中的代码禁用测试似乎可以解决您的问题,但我认为您应该安装Windows版本的file
并重新启用该欺骗检查。
完成该问题后,您发现Paperclip::Errors::NotIdentifiedByImageMagickError
即将出现;我想那是因为当您安装ImageMagick时,您仍然需要在Rails环境中设置Paperclip的command_path
配置选项,以便它可以找到它。
答案 2 :(得分:0)
我发现这是一个老问题,已找到最佳答案,但我最近遇到了同样的问题并通过更新development.rb
中的原始Paperclip.options信息来解决问题: / p>
Paperclip.options[:command_path] = 'C:\Program Files\ImageMagick-6.9.0-Q16;C:\Program Files (x86)\GnuWin32\bin;'
您必须将ImageMagick和File添加到路径中。省略对File的引用会产生原始引用的错误消息"Avatar has an extension that does not match its contents"
,但省略ImageMagick会产生Matt Gibson提到的错误消息,"Paperclip::Errors::NotIdentifiedByImageMagickError."
我不完全确定这是File未正确安装或安装的情况,而是没有人明确提到需要将两个引用添加到PATH。我知道对于已经编码了一段时间的人来说,这似乎是一种相对“常识”类型的问题,但对于一个新手来说,有时这样的事情需要拼写出来。我只是想出来因为我使用了一个SUPER OLD教程在我的机器上安装Ruby / Rails并且非常熟悉PATH和命令行,而新的教程没有详细介绍这类东西而是想要让你“尽快启动并运行......”即使副作用是因为你不一定了解你在做什么。
答案 3 :(得分:0)
Paperclip 3.5.4也适用于您的代码