【问题标题】:I can't seem to get pictures to upload with paperclip, I don't get an error after uploading我似乎无法使用回形针上传图片,上传后我没有收到错误消息
【发布时间】:2015-02-12 07:32:13
【问题描述】:

我不确定它是不是 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"

结束

【问题讨论】:

  • :storage =&gt; :fog, 为什么需要设置雾凭据?
  • 我不这么认为,但在我有那个设置之前它甚至都没有工作。
  • 你是否按照你需要的Windows 7安装步骤file.exe从这里gnuwin32.sourceforge.net/packages/file.htm然后设置环境变量你这样做了吗?
  • 我没有,你能解释一下为什么我什至需要使用雾吗?如果我不需要,有没有办法设置它以便我仍然可以上传图片?
  • 我正在为你在 windows 7 上发帖

标签: ruby-on-rails imagemagick paperclip fog


【解决方案1】:

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/environments/*.rbconfig.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 中使用回形针

【讨论】:

  • 这非常有效!唯一的问题是我必须从 users_path, :html => { :multipart => true } do |form| 更改您给我的原始 html 代码%> 我把它改成了我原来的那个,它工作了 { :multipart => true } do |f| %>
  • 只有一个问题,在哪里可以为没有头像的新用户放置默认图片?
  • 如果所有用户都一样,那么您可以简单地使用assets/images 文件夹来使用图像
  • 这根本不起作用,我将它设置为正确的路径仍然没有。
  • 在资产中?文件夹你保存了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 2014-08-24
相关资源
最近更新 更多