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

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

提问人:Tín Ngô 提问时间:6/25/2023 最后编辑:jvillianTín Ngô 更新时间:6/28/2023 访问量:80

问:

这是代码,它仍然进行交易,一切似乎都很好

// 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", "[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)

但我不知道为什么它不附加

请帮忙,我试图找到同样的问题,但似乎它只是发生在我身上=(( PS:对不起,我的英语不好

HTML Ruby-on-Rails 图像 文件 Rails-ActiveStorage

评论

0赞 Les Nightingill 6/26/2023
表达式看起来不正确。你真的有模型吗?通常,我希望在 application_controller.rb 中定义类似 where 的东西Current.user.avatar.attach...Currentcurrent_user.avatar.attach...current_user
0赞 Tín Ngô 6/26/2023
class Current < ActiveSupport::CurrentAttributes attribute :user end // 我正在使用 Rails 7,所以它可能是一个新功能(idk,我是从 youtube 上学到的)
0赞 LihnNguyen 6/26/2023
我认为你应该被用来替换.但是,使用那是okie。 习惯于在模型中。让我们在单击“提交表单”时显示日志。current_userCurrent.userCurrent.userCurrent.user
0赞 dbugger 6/26/2023
将日志添加到问题中 --> 而不是评论。评论不是永久性的。
0赞 Tín Ngô 6/26/2023
我已经添加了日志

答:

0赞 tyrro 6/28/2023 #1

从您共享的日志来看,它似乎在哈希值中。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']

评论

0赞 Tín Ngô 7/3/2023
谢谢,我修复了它,按照你说的做了,或者我只是从表单中删除模型并继续使用 params[:avatar]
0赞 tyrro 7/12/2023
如果答案解决了您的问题,您可以将答案标记为已接受,谢谢!