配置文件的未知属性 'avatar'。 Rails 5

unknown attribute 'avatar' for Profile. Rails 5

当我运行迁移时,

已创建:

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

然后添加:

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

观看次数。 当我上传以创建配置文件时,出现错误(在标题中)。

我的 profiles_controller:

中有这个定义
private
def profile_params
  params.require(:profile).permit(:first_name, :last_name, :avatar, :phone_number, :contact_email, :description)
end

我也在 application_controller 中添加了以下内容:

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

users_controller

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

此后仍然出现错误。

感谢任何帮助。

谢谢

更新:

["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 - 我做了 运行 迁移

更新:

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

unknown attribute 'avatar' for Profile

问题在于您的 Profile 模型内部有一个 User 模型。您应该删除 User 模型并保留其余模型。

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