2015-02-12 73 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

相關問題