简体   繁体   中英

“Unknown attribute: avatar” when uploading image with Paperclip?

I ran this migration:

rails generate paperclip user avatar

It created this migration file:

class AddAttachmentAvatarToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.attachment :avatar
    end
  end

  def self.down
    drop_attached_file :users, :avatar
  end
end

I added this to my edit user registration view:

<div class="form-group">
  <%= f.label :avatar %> <br>
  <%= f.file_field :avatar, :autofocus => true, class: 'form-control' %>
</div>

When I try to upload an avatar in edit user registration, I receive this error:

ActiveRecord::UnknownAttributeError in Devise::RegistrationsController#update unknown attribute: avatar

EDITS

I added

    def user_params
      params.require(:user).permit(:avatar)
    end

to my user model and now I don't get the error, but now the profile picture just prints out as a link. I think I can find an answer to that.

Are you sure you're permitting the :avatar attribute through your controller's permits? Also, have you mentioned the following line of code in your model?

has_attached_file :avatar

Added

def user_params
  params.require(:user).permit(:avatar)
end

to my UsersController to fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM