我有一个烦人的问题。我正在建立一个简单的社交网络,它有一个会员模式。我使用Devise进行身份验证,使用CanCan进行授权。我还创建了一个阿凡达模型,它与会员模型有一对一的关系。我使用Carrierwave上传头像。一切正常,在成功编辑个人资料后,当我访问个人资料显示或索引视图(位于视图/成员文件夹中)时,我可以看到上传的头像。但当我再次访问配置文件编辑页面时,它会消失(也会从上传文件夹中删除),而我没有故意删除它,也没有保存编辑表单。
我导入了所有的设计视图,以便能够自定义它们。
我使用views/members/registrations/edit.html.erb作为成员配置文件编辑视图,其中包含以下表单:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div class="invisible">
<%= resource.build_avatar %>
</div>
<div id="avatar">
<figure id="avatar_pic">
<% if @member.avatar.nil? %>
<img src="/assets/profile_pic.gif" alt="<%= @member.name %>">
<% else %>
<%= image_tag(@member.avatar.avatar_url, :alt => "#{@member.name}") %>
<% end %>
<%= f.fields_for :avatar do |avatar| %>
<%= avatar.file_field :avatar %>
<% end %>
</figure>
<div id="user_data">
<%= f.text_field :name, :autofocus => true %>
<p><%= f.text_field :age, :placeholder => "Életkor" %></p>
<p><%= f.text_field :city, :placeholder => "Település" %></p>
<p><%= f.text_field :job, :placeholder => "Foglalkozás" %></p>
</div>
</div>
<div id="user_status">
<div id="kms">
<p id="finish_date">2013. október 31.</p>
<%= f.text_field :kms %> <br>
<%= f.text_field :amount %>
<%= f.password_field :current_password, :placeholder => "Mentéshez adja meg jelszavát!" %>
</div>
<%= f.submit "Save" %>
</div>
<% end %>
我还导入了Devise Registrations Controller,因为我需要定制一些东西。例如,要实现jCrop。现在看起来是这样的:
class Members::RegistrationsController < Devise::RegistrationsController
before_filter :check_permissions, :only => [:update]
skip_before_filter :require_no_authentication
def check_permissions
authorize! :update, resource
end
def create
super
end
def update
super
end
end
我的会员模型看起来是这样的(我在has_one:avantar之后进行了依赖性销毁,但为了测试原因而删除了它,但似乎没有):
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_permalink :name
has_one :avatar
accepts_nested_attributes_for :avatar
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :kms, :amount, :age, :job, :city, :why, :permalink, :avatar_attributes
validates_uniqueness_of :email
end
当我访问会员编辑页面时的消息,之前上传了一个头像:
ActionController::RoutingError (No route matches [GET] "/assets"):
actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.5) lib/rack/lock.rb:15:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
railties (3.2.13) lib/rails/engine.rb:479:in `call'
railties (3.2.13) lib/rails/application.rb:223:in `call'
rack (1.4.5) lib/rack/content_length.rb:14:in `call'
railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Rendered /usr/local/rvm/gems/ruby-2.0.0-p0/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.9ms)
知道为什么当我访问成员编辑页面时,Rails会删除(从数据库和服务器)Avatar吗?
祝福,Matt
好的。看来我想明白了。需要将以下代码放入设计注册_controller.rb.中
def edit
resource.avatar || resource.build_avatar
super
end