Search code examples
htmlruby-on-railsimagefilerails-activestorage

There's no error appear, but still can't attach file, ruby on rails


this is the code, it still comit transaction and everything seems okay

// controller
def avatar_create
  Current.user.avatar.attach(params[:avatar])
  if Current.user.avatar.attached?
    redirect_to root_path
  else
    flash[:alert] = "some thing was wrong"
    render :avatar_new, status: :unprocessable_entity
  end
end


//erb
<%= form_with model: Current.user, url: avatar_path, method: :post do |form|%>
  <%= form.file_field :avatar%>
  <%= form.submit "post"%>
<%end%>

and the log

Started POST "/avatar" for ::1 at 2023-06-26 22:23:06 +0700
Processing by UserController#avatar_create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f9b30875500 @tempfile=#<Tempfile:/tmp/RackMultipart20230626-4800-tdl8hi.jpeg>, @content_type="image/jpeg", @original_filename="hello.jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"hello.jpeg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Tạo ảnh đại diện"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
  ↳ app/helpers/user_helper.rb:4:in `current_user?'
  TRANSACTION (0.1ms)  begin transaction
  ↳ app/controllers/user_controller.rb:29:in `avatar_create'
  User Exists? (0.5ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? AND "users"."id" != ? LIMIT ?  [["email", "[email protected]"], ["id", 4], ["LIMIT", 1]]
  ↳ app/controllers/user_controller.rb:29:in `avatar_create'
  ActiveStorage::Attachment Load (0.2ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ?  [["record_id", 4], ["record_type", "User"], ["name", "avatar"], ["LIMIT", 1]]
  ↳ app/controllers/user_controller.rb:29:in `avatar_create'
  TRANSACTION (0.1ms)  commit transaction
  ↳ app/controllers/user_controller.rb:29:in `avatar_create'
  Rendering layout layouts/application.html.erb
  Rendering user/avatar_new.html.erb within layouts/application
  Rendered user/avatar_new.html.erb within layouts/application (Duration: 1.8ms | Allocations: 424)
  Rendered apart/_navbar.html.erb (Duration: 1.4ms | Allocations: 377)
  Rendered layout layouts/application.html.erb (Duration: 27.6ms | Allocations: 7809)
Completed 422 Unprocessable Entity in 56ms (Views: 28.8ms | ActiveRecord: 1.2ms | Allocations: 13194)

but i don't know why it doesn't attach

please help, i tried to find the same problem but seems it just happen to me =(( ps: sorry for my bad english


Solution

  • From the logs you shared, it seems avatar is inside the user hash.

      Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f9b30875500 @tempfile=#<Tempfile:/tmp/RackMultipart20230626-4800-tdl8hi.jpeg>, @content_type="image/jpeg", @original_filename="hello.jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"hello.jpeg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Tạo ảnh đại diện"}
    

    So, updating params[:avatar] to params['user']['avatar'] should solve your issue.