代码之家  ›  专栏  ›  技术社区  ›  Steven Aguilar

ActiveModel::UnknownAttributeError未知属性“avatar”-ActiveStorage

  •  1
  • Steven Aguilar  · 技术社区  · 7 年前

    目前我正在尝试附加一个 :avatar 配置文件上的字段。我得到以下错误: enter image description here

    然而,在这些文档之后,我添加了 轮廓。

    models/profile.rb
    class Profile < ApplicationRecord
      belongs_to :user
      has_one_attached :avatar
    end
    

    :阿凡达 在坚强的情怀里。在配置文件控制器中。

    class ProfilesController < ApplicationController
      def create
        @profile = current_user.create_profile(profile_params)
      end
      ##
      private
    
      def profile_params
         params.require(:profile).permit(:full_name, :city, :bio, :avatar)
      end
    end
    

    class User < ApplicationRecord
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :validatable
      has_many :photos
      has_one :profile
    end
    

    我请求 profiles/new.html.erb 作为文件附件:

    <%= form_for @profile do |f| %>
      <div class="form-group">
        <%= f.label :avatar %>
        <%= f.file_field :avatar, as: :file, class: "form-control" %>
      </div>
    

    :阿凡达

    Started POST "/profiles" for 127.0.0.1 at 2018-09-22 17:55:26 -0400
    Processing by ProfilesController#create as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"CXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX==", "profile"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x007ff62db322b8 @tempfile=#<Tempfile:/var/folders/86/g7_xcx392qn815kmkcm55ydc0000gn/T/RackMultipart20180922-10194-awlxd1.png>, @original_filename="profile.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"profile[avatar]\"; filename=\"profile.png\"\r\nContent-Type: image/png\r\n">
    

    我可以上传另一个型号的附件,所以这不是安装的问题。知道我为什么会犯这个错误吗?

    0 回复  |  直到 7 年前
    推荐文章