Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 315
Dylan Richards Avatar asked Nov 30 '13 17:11

Dylan Richards


2 Answers

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
like image 76
RageCore Avatar answered Dec 04 '22 13:12

RageCore


Added

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

to my UsersController to fix the issue.

like image 26
Dylan Richards Avatar answered Dec 04 '22 13:12

Dylan Richards