代码之家  ›  专栏  ›  技术社区  ›  Linards Berzins

配置文件的未知属性“avatar”。轨道5

  •  -2
  • Linards Berzins  · 技术社区  · 7 年前

    这是创建的:

    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' %>
    

    我有这个def 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
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Pavan Cesar    7 年前

    配置文件的未知属性“avatar”

    问题是你的 Profile 模型具有 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