2015-11-01 51 views
0

Ruby版本2.1.5和rails 4.1.8。CarrierWave不能正常工作

所以我正在開發一個應用程序,使用設計進行身份驗證過程。我需要添加一個頭像選項,用戶可以上傳圖片,爲此我正在使用carrierwave。我遵循here給出的所有說明,但是頭像並沒有在任何地方保存。

這裏是我的avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base 
    storage :file 
    def store_dir  
     "public/uploads" 
    end 
end 

這裏是我的模型user.rb

class User < ActiveRecord::Base 
mount_uploader :avatar, AvatarUploader 

devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, 
    :validatable, :confirmable, :timeoutable 

attr_accessor :login, :avatar, :avatar_cache, :remove_avatar 

validates :username, presence: true, uniqueness: true 

validate :email 

def email_required? 
    false 
end 

def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first 
    else 
     where(conditions).first 
    end 
end 
end 

這裏是我的index.html.erb

<h2>Sign up</h2> 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:multipart => true}) do |f| %> 
<%= devise_error_messages! %> 

<div class="field"> 
<%= f.label :username %> 
<% if Own_settings.minimum_username_length %> 
<em>(<%= Own_settings.minimum_username_length %> characters minimum)</em> 
<% end %><br /> 
<%= f.text_field :username, autofocus: true%> 
</div> 

<div class="field"> 
<%= f.label :email %> 
<em>(Optional)</em><br /> 
<%= f.email_field :email %> 
</div> 

<div class="field"> 
<%= f.label :password %> 
<% if @minimum_password_length %> 
<em>(<%= @minimum_password_length %> characters minimum)</em> 
<% end %><br /> 
<%= f.password_field :password, autocomplete: "off" %> 
</div> 

<div> 
<%= f.file_field :avatar %><br /> 
<%= f.hidden_field :avatar_cache %><br /> 
</div> 

<div class="actions"> 
<%= f.submit "Sign up" %> 
</div> 
<% end %> 

<%= render "devise/shared/links" %> 

這裏是我的edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, multipart: :true }) do |f| %> 
<%= devise_error_messages! %> 

<div> 
<% if current_user.avatar.url.present? %> 
    <%= image_tag current_user.avatar.url.to_s %> 
    <%= f.label :remove_avatar %> 
    <%= f.check_box :remove_avatar %> 
<% end %> 
<%= f.file_field :avatar %> 
<%= f.hidden_field :avatar_cache %> 
</div> 

<div class="field"> 
<%= f.label :username %> 
<% if Own_settings.minimum_username_length %> 
<em>(<%= Own_settings.minimum_username_length %> characters minimum)</em> 
<% end %><br /> 
<%= f.text_field :username, autofocus: true%> 
</div> 

<div class="field"> 
<%= f.label :email %><br /> 
<%= f.email_field :email, autofocus: true %> 
</div> 

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %> 
<div/> 
<% end %> 

<div class="field"> 
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
<%= f.password_field :password, autocomplete: "off" %> 
</div> 

<div class="field"> 
<%= f.label :password_confirmation %><br /> 
<%= f.password_field :password_confirmation, autocomplete: "off" %> 
</div> 

<div class="field"> 
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
<%= f.password_field :current_password, autocomplete: "off" %> 
</div> 

<div class="actions"> 
<%= f.submit "Update" %> 
</div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p> 

<%= link_to "Back", :back %> 

[這是錯誤來] [1]

[1]:http://i.stack.imgur.com/ulH9j.png當我嘗試調用編輯頁面。

回答

0

在虛擬形象上傳之前,沒有分配給用戶的虛擬形象。首先你必須檢查是否有任何頭像。你的情況應該是這樣的 current_user.avatar.presence?current_user.avatar.url.presence?

+0

還好你是對的,但問題不在這裏,我的頭像沒有得到任何地方保存,當我上傳到註冊頁面上 –

0

我認爲這與你的avartar_uploader.rb文件,你如何不包括RMagick或MiniMagick做支撐。另外,你的存儲方向將不起作用,所以我包含了適用於我的store_dir。

您需要添加/修復以下代碼。

class AvatarUploader < CarrierWave::Uploader::Base 
    include CarriierWave:RMagick 
    storage :file 

    def store_dir  
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
end 

您還可以在線使用Cloudinary進行文件存儲。它適用於CarrierWave。 Cloudinary是一項基於雲計算的服務,提供端到端的圖像管理解決方案,包括上傳,存儲,管理,圖像處理和交付。

這裏是文檔http://cloudinary.com/documentation/rails_carrierwave