ruby-on-rails 没有出现错误,但仍然无法附加文件,ruby on rails

ne5o7dgx  于 2023-07-01  发布在  Ruby
关注(0)|答案(1)|浏览(174)

这是代码,它仍然提交事务,一切似乎都很好

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

和日志

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", "tigopro.1703@gmail.com"], ["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)

但我不知道为什么它不连接
请帮助,我试图找到同样的问题,但似乎它只是发生在我身上=((ps:对不起我的英语不好

g6baxovj

g6baxovj1#

从您共享的日志来看,似乎avataruser哈希中。

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

因此,将params[:avatar]更新为params['user']['avatar']应该可以解决您的问题。

相关问题