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

I can't seem to get pictures to upload with paperclip, I don't get an error after uploading

我不确定它是否具有 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

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

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

this URL下载并安装文件 要进行测试,您可以使用以下内容:untitled

Paperclip 以 gem 的形式分发,这就是它在您的应用中的使用方式。

接下来,您需要与您的环境集成 - 最好通过 PATH 变量,或更改您的 config/environments/development.rb 文件

路径

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

环境

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) %>

config.paperclip_defaults 上的

config/environments/*.rb 个文件,当您的 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 中也可以使用回形针