RubyVersion2.1.5和rails 4.1.8。
因此,我正在开发一个应用程序,使用devise身份验证过程。我需要添加一个阿凡达选项,用户可以上传一个图像,为此,我正在使用载波。我遵循给这里的所有指令,但是化身没有保存任何地方。
这是我的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:当我试图调用编辑页面时,http://i.stack.imgur.com/ulH9j.png。
发布于 2015-11-01 21:36:29
在阿凡达上传之前,没有分配给用户的化身。首先,你必须检查是否有任何化身。您的情况应该类似于这个current_user.avatar.presence?而不是current_user.avatar.url.presence?
发布于 2015-11-02 19:08:39
我认为这与您的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是一种基于云的服务,它提供端到端的图像管理解决方案,包括上传、存储、管理、图像操作和传送。
这是文档载波
https://stackoverflow.com/questions/33467119
复制相似问题