我似乎无法使用回形针上传图片,上传后我没有收到错误

时间:2015-02-12 07:32:14

标签: ruby-on-rails imagemagick paperclip fog

我不确定它的图像是否有效!

这是在我的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"

1 个答案:

答案 0 :(得分:0)

Windows 7的回形针

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

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

下载&amp;从this URL安装文件 要进行测试,您可以使用以下内容:无标题

enter image description here

Paperclip作为gem发布,应该在你的应用中使用它。

接下来,您需要与您的环境集成 - 最好通过PATH变量,或通过更改您的config / environments / 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:

gem "paperclip", "~> 4.2"

在模型中

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个文件,当您的Rails应用启动时,这些文件将合并到Paperclip::Attachment.default_options。一个例子:

  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中起作用回形针