Currently I'm trying to attach an :avatar
field on a Profile. I get the following error:
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?
rails server
restarts Spring? or how can I restart Spring? Yes, I reload the server – Xylographrails server
loads faster. Try runningspring stop
from the root of your app (same folder that you'd runrails server
from), and then runrails server
again and see if that fixes your issue. – Mediation