CarrierWave不工作

时间:2015-11-01 20:56:59

标签: ruby-on-rails devise carrierwave

Ruby版本2.1.5和rails 4.1.8。

所以我正在开发一个app,使用devise进行身份验证过程。我需要添加一个头像选项,用户可以上传图像,为此我使用的是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当我尝试调用编辑页面时。

2 个答案:

答案 0 :(得分:0)

在头像上传之前,没有分配给用户的头像。首先,你必须检查是否有任何头像。你的病情应该是这样的 current_user.avatar.presence?不是current_user.avatar.url.presence?

答案 1 :(得分: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