2015-02-12 79 views
0

我不知道它的imagemagick或不!我似乎无法得到图片上传回形针,我没有得到错误后上传

这是在我的User.rb,我没有得到任何错误,只是一个空白的图像!请让我知道我该如何解决这个问题!

class User < ActiveRecord::Base 
    has_secure_password 
    validates :email, :name, presence: true, uniqueness: true 
    validates_inclusion_of :age, in: 10..100 
    validates :password, presence: true 
    has_attached_file :profile_picture, 
      :path => ":rails_root/public/system/:attachment/:id/:style/:filename", 
      :url => "/system/:attachment/:id/:style/:filename", 
      :storage => :fog, 
      :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
      :default_url => "C:\row\website\public\images" 

end 

这是我Development.rb,

Rails.application.configure do 

    config.cache_classes = false 

    config.eager_load = false 

    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    config.action_mailer.raise_delivery_errors = false 

    config.active_support.deprecation = :log 

    config.active_record.migration_error = :page_load 

    config.assets.debug = true 

    config.assets.digest = true 

    config.assets.raise_runtime_errors = true 

    Paperclip.options[:command_path] = "C:\ImageMagick" 

为Windows 7

+0

':存储=>:雾,'为什么需要它你设置雾凭据? – 2015-02-12 07:44:25

+0

我不这么认为,即使我有这样的设定,它也没有工作。 – genobambino 2015-02-12 07:45:03

+0

你是否遵循windows 7的安装步骤,你需要'file.exe'从这里http://gnuwin32.sourceforge.net/packages/file.htm然后设置env变量你做到了这一点? – 2015-02-12 07:46:14

回答

0

回形针如果您使用Windows 7+作为开发环境,你可能需要手动安装file.exe应用程序。 Paperclip 4+中的文件欺骗系统依赖于此;如果您没有工作,您将收到验证失败:上传文件的扩展名与其内容不匹配。错误。

要手动安装,应执行以下操作:

下载&从this URL 安装文件来测试,您可以使用以下方法:无

enter image description here

回形针分布作为宝石,这是它应该如何在您的应用程序中使用。

接下来,你需要与你的环境集成 - 通过preferrably PATH变量,或通过改变你的config /环境/ development.rb文件

PATH

1. Click "Start" 
2. On "Computer", right-click and select "Properties" 
3. In properties, select "Advanced System Settings" 
4. Click the "Environment Variables" button 
5. Locate the "PATH" var - at the end, add the path to your newly installed `file.exe` (typically `C:\Program Files (x86)\GnuWin32\bin`) 
6. Restart any CMD shells you have open & see if it works 

OR

环境

1. Open `config/environments/development.rb` 
2. Add the following line: `Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'` 
3. Restart your Rails server 
Either of these methods will give your Rails setup access to the file.exe functionality, this providing the ability to check the contents of a file (fixing the spoofing problem) 

将宝石包含在您的Gemfile中:

gem "paperclip", "~> 4.2" 

在MODEL

class User < ActiveRecord::Base 
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png", :path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename" 
    validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ 

end 

迁移

class AddAvatarColumnsToUsers < ActiveRecord::Migration 
    def self.up 
    add_attachment :users, :avatar 
    end 

    def self.down 
    remove_attachment :users, :avatar 
    end 
end 

浏览

<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %> 
    <%= form.file_field :avatar %> 
<% end %> 

控制器

def create 
    @user = User.create(user_params) 
end 

private 

# Use strong_parameters for attribute whitelisting 
# Be sure to update your create() and update() controller methods. 

def user_params 
    params.require(:user).permit(:avatar) 
end 

显示浏览

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

对于FOG

config/environments/*.rb文件上config.paperclip_defaults,这些都将获得合并为Paperclip::Attachment.default_options为您的Rails应用程序的靴子。举个例子:

module YourApp 
    class Application < Rails::Application 
    # Other code... 

    config.paperclip_defaults = {:storage => :fog, :fog_credentials => {:provider => "Local", :local_root => "#{Rails.root}/public"}, :fog_directory => "", :fog_host => "localhost"} 
    end 
end 

,也将努力回形针在Win 7

+0

这工作非常好!唯一的问题是我不得不改变你给我的原始HTML代码,从<%= form_for @user,:url => users_path,:html => {:multipart => true} do | form | %> 我将它更改为我的原始版本,并且它可以正常工作 <%= form_for(At symbol)user,:html => {:multipart => true} do | f | %> – genobambino 2015-02-12 08:34:05

+0

很高兴听到这个消息 – 2015-02-12 09:07:02

+0

只有一个问题,我可以在哪里为任何没有个人资料图片的新用户设置默认图片? – genobambino 2015-02-12 09:14:09

相关问题