我正在尝试在Devise用户注册表单上创建图片上传,并在我提交时不保存表单。我已将其设置如下:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
#has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
has_attached_file :avatar, styles: {
large: "600x450#",
medium: "250x250#",
small: "100x100#"
}, :default_url => "/images/:style/filler.png"
validates_attachment_content_type :avatar, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
validates :avatar, presence: true
end
users_controller.rb
class UsersController < ApplicationController
def index
@users = User.order('created_at DESC').all
end
def create
@user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:avatar)
end
end
/views/users/registrations/new.html.erb
<h2>Sign up yeah</h2>
<%= simple_form_for(resource, :as => resource_name, :html => { :multipart => true }, :url => registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, :required => true, :autofocus => true %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<%= f.file_field :avatar %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
在我的开发日志中,我打印出错误:
Started POST "/users" for 127.0.0.1 at 2014-08-01 15:15:06 +0100
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"v/lf0StTLDOtdGtBynnecKm4fVL92+j2N9vepCvAW4U=", "user"=>{"email"=>"mdunbavan@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00000102c02800 @tempfile=#<File:/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/RackMultipart20140801-1710-n7njjc>, @original_filename="2.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"2.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Sign up"}
Command :: file -b --mime '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-1mj70uf.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-di3eje.png[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
[1m[35m (0.3ms)[0m begin transaction
[1m[36mUser Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "users" WHERE "users"."email" = 'mdunbavan@gmail.com' LIMIT 1[0m
Command :: file -b --mime '/var/folders/mf/srx7jt8s2rdg0mn5hr98cvz80000gn/T/c81e728d9d4c2f636f067f89cc14862c20140801-1710-1dimcon.png'
[1m[35m (0.3ms)[0m rollback transaction
我在我的机器上安装了imagemagick并且工作正常,但我根本不理解这个错误。
有人可以帮忙吗?
答案 0 :(得分:1)
当您在应用程序中使用Devise
作为身份验证解决方案时,除非另有说明,否则Devise自己的控制器将用于执行所有操作。如果您注意到服务器日志
Processing by Devise::RegistrationsController#create as HTML
用户创建请求由Devise的RegistrationsController
而非UsersController
处理。建议您仅在需要进行极端自定义时为Devise模型创建自己的控制器。
在您的情况下,您需要做的是将avatar
属性列入白名单,以便Devise知道您打算将其保存在数据库中。
执行此操作的最佳方法是通过initializer
# config/initializers/devise_permitted_parameters.rb
module DevisePermittedParameters
extend ActiveSupport::Concern
included do
before_action :configure_permitted_parameters
end
protected
def configure_permitted_parameters
## To permit attributes while registration i.e. sign up
devise_parameter_sanitizer.for(:sign_up) << :avatar
## To permit attributes while editing a registration
devise_parameter_sanitizer.for(:account_update) << :avatar
end
end
DeviseController.send :include, DevisePermittedParameters
-OR -
另一种方法是更新ApplicationController
,如下所示:
class ApplicationController < ActionController::Base
#...
## Add a callback
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
## To permit attributes while registration
devise_parameter_sanitizer.for(:sign_up) << :avatar
## To permit attributes while editing a registration
devise_parameter_sanitizer.for(:account_update) << :avatar
end
end
这种方法的缺点是它会增加额外的开销,因为会对应用程序的每个请求进行评估,以确定它是否适用于Devise
控制器。
注意:强>
为了更好地了解Devise
与Rails
的使用情况,我建议您通过 Daniel Kehoe 查看一个很棒的Rails and Devise示例应用程序。
<强>更新强>
运行which convert
命令以获取安装ImageMagick
的路径。
例如,如果它返回/usr/local/bin/convert
,则对于开发模式,将以下行添加到config/environments/development.rb
中:
Paperclip.options[:command_path] = "/usr/local/bin/"