-2

When I run migration,

this got created:

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

 def self.down
   remove_attachment :profiles, :avatar
 end
end

then added:

<%= f.label :avatar %>
<%= f.file_field :avatar, :autofocus => true, class: 'form-control' %>

to views. When I upload to create a profile I get an error (in title).

I have this def in my profiles_controller:

private
def profile_params
  params.require(:profile).permit(:first_name, :last_name, :avatar, :phone_number, :contact_email, :description)
end

I have added the following in my application_controller too:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:user) << :avatar
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:avatar) }
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:avatar) }  
end

And users_controller:

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

Still getting an error after this.

Any help appreciated.

Thanks

UPDATE:

["id", "user_id", "first_name", "last_name", "phone_number", 
"contact_email", "description", "avatar_file_name", 
"avatar_content_type", "avatar_file_size", "avatar_updated_at"]

@Pavan - I did run migrate

UPDATE:

class Profile < ActiveRecord::Base
 belongs_to :user

 class User < ActiveRecord::Base
  has_attached_file :avatar, styles: { medium: "300x300>", thumb: 
  "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :avatar, content_type: 
  /\Aimage\/.*\z/
 end
end
Linards Berzins
  • 83
  • 4
  • 7
  • 20

1 Answers1

2

unknown attribute 'avatar' for Profile

The problem is with your Profile model having a User model inside it. You should remove the User model and keep the rest.

class Profile < ActiveRecord::Base
  belongs_to :user

  has_attached_file :avatar, styles: { medium: "300x300>", thumb: 
  "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :avatar, content_type: 
  /\Aimage\/.*\z/
end
Pavan
  • 33,316
  • 7
  • 50
  • 76