ActiveModel::UnknownAttributeError unknown attribute 'avatar' - ActiveStorage
Asked Answered
X

0

4

Currently I'm trying to attach an :avatar field on a Profile. I get the following error: enter image description here

However, following the docs I added the relationshipt of :avatar to Profile.

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

I also add :avatar in the strong params. In the Profile controllers.

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

I wonder if the issue is because of the association between User and Profile. A user has one profile and profile belongs to user.

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

I request in the profiles/new.html.erb as a file attachement:

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

I see that in the params that the file is in avatar which is why I find so confusing that it doesn't recognize :avatar as an attribute.

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

I was able to upload attachement for another model so it's not something with the installation. Any ideas why I'm getting this error?

Xylograph answered 23/9, 2018 at 18:46 Comment(7)
I would make sure that you've killed your server, and if you're using Spring, try restarting that. I've had plenty of weird issues with Rails 5 because code reloading didn't work correctly.Mediation
@Mediation does restaring rails server restarts Spring? or how can I restart Spring? Yes, I reload the serverXylograph
The spring process starts when you first boot up the rails app, and just keeps running in the background, making subsequent rails server loads faster. Try running spring stop from the root of your app (same folder that you'd run rails server from), and then run rails server again and see if that fixes your issue.Mediation
@nate that wasn't itXylograph
Bummer. Sorry, not sure then. I've never used Active Storage.Mediation
Are you still having this problem.Solorzano
Are you using rails 6.0.0.rc1 and ActiveStorage::Downloading?Shirleneshirley

© 2022 - 2024 — McMap. All rights reserved.