1

我有一个烦人的问题。我正在构建一个具有会员模型的简单社交网络。我使用 Devise 进行身份验证,使用 CanCan 进行授权。我还创建了一个 Avatar 模型,它与 Member 模型具有一对一的关系。我使用 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 RegistrationsController,因为我需要自定义一些东西。例如,要实现 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 :avatar 之后有依赖破坏,但删除它以测试导致问题的原因,但似乎没有):

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 吗?

最好的祝愿,马特

4

1 回答 1

1

好的。似乎我想通了。需要将下面的代码放在 devise registrations_controller.rb 中。

def edit
    resource.avatar || resource.build_avatar
    super
end
于 2013-07-29T14:42:49.267 回答