Search code examples
ruby-on-railsimagemagickpaperclipfog

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


I'm not sure if its imagemagick or not!

This is in my User.rb, I don't get any errors, Just a blank image! Please let me know how I can fix this!

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

This is in my 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"

end


Solution

  • Paper clip for windows 7

    If you're using Windows 7+ as a development environment, you may need to install the file.exe application manually. The file spoofing system in Paperclip 4+ relies on this; if you don't have it working, you'll receive Validation failed: Upload file has an extension that does not match its contents. errors.

    To manually install, you should perform the following:

    Download & install file from this URL To test, you can use the following: untitled

    enter image description here

    Paperclip is distributed as a gem, which is how it should be used in your app.

    Next, you need to integrate with your environment - preferrably through the PATH variable, or by changing your config/environments/development.rb file

    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

    Environment

    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)
    

    Include the gem in your Gemfile:

    gem "paperclip", "~> 4.2"
    

    In 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
    

    Migration

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

    Views

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

    Controller

    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
    

    Show Views

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

    For FOG

    config/environments/*.rb files on config.paperclip_defaults, these will get merged into Paperclip::Attachment.default_options as your Rails app boots. An example:

      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
    

    And it will work paper clip in win 7