1

我想为我的用户模型添加多个不同的附件,例如头像和封面照片。我正在使用回形针。

目前可以上传新头像,但每次我想更新封面照片时,我都会收到一个错误You are already signed in.。我正在使用设计进行身份验证。

我的模型:

# Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :username, :login, :avatar

  has_many :wishes

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  validates_uniqueness_of :username

  # hide instead of deleting
  acts_as_paranoid

  # tracking
  # include PublicActivity::Model
  # tracked owner: Proc.new{ |controller, model| controller.current_user }

  # Avatar - Paperclip
  has_attached_file :avatar,
                    :styles => { 
                      :extra_large => "600x600#",
                      :large => "400x400#",
                      :medium => "250x250#",
                      :small => "145x145#",
                      :tiny => "45x45#",
                      :icon => "16x16#"
                    },
                    :default_url => '/assets/default-user-avatar/:style.jpg'

  # Avatar - Paperclip
  has_attached_file :cover_photo,
                    :styles => { 
                      :large => "940x360#",
                      :extra_large => "1880x720#"
                    }

  # Versions
  has_paper_trail

  searchable do
    text :username, :boost => 5
    text :firstname
    text :lastname
    text :email
  end

和形式:

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => ({ :method => :put, :multipart => true })) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :firstname %>
    <%= f.input :lastname %>
    <%= f.input :username, :wrapper => :prepend do %>
      <span class="add-on">@</span>
      <%= f.input_field :username %>
    <% end %>
    <%= f.input :email, :required => true, :autofocus => true %>
    <%= f.input :password, :autocomplete => "off", :hint => "leave it blank if you don't want to change it", :required => false %>
    <%= f.input :password_confirmation, :required => false %>
    <%= f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true %>

    Avatar:<br/>
    <%= f.file_field :avatar %><br/>
    Cover Photo:<br/>
    <%= f.file_field :cover_photo %><br/>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Update" %>
  </div>
<% end %>

希望你们能帮助我!

4

1 回答 1

0

您可能希望将 :cover_photo 添加到此行:

 attr_accessible :email, :password, :password_confirmation, :remember_me, :firstname, :lastname, :username, :login, :avatar, :cover_photo

...并确保您已在数据库中设置了 cover_photo 列

于 2013-10-22T08:18:18.883 回答