没有错误显示,但仍然无法附加文件,Ruby on Rails。

huangapple go评论59阅读模式
英文:

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

问题

这是代码,它仍然提交事务,一切都看起来正常

// 控制器
def avatar_create
  Current.user.avatar.attach(params[:avatar])
  if Current.user.avatar.attached?
    redirect_to root_path
  else
    flash[:alert] = "一些东西出错了"
    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 "发布" %>
<% end %>

和日志

开始 POST "/avatar" 到 ::1 于 2023-06-26 22:23:06 +0700
由 UserController#avatar_create 处理为 TURBO_STREAM
参数: {"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) 开始事务
↳ 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) 提交事务
↳ app/controllers/user_controller.rb:29:in `avatar_create'
渲染 layout layouts/application.html.erb
渲染 user/avatar_new.html.erb 在 layouts/application 内
在 layouts/application 内渲染 user/avatar_new.html.erb (持续时间: 1.8ms | 分配: 424)
渲染了 apart/_navbar.html.erb (持续时间: 1.4ms | 分配: 377)
渲染 layout layouts/application.html.erb (持续时间: 27.6ms | 分配: 7809)
在 56ms 内完成 422 无法处理的实体 (视图: 28.8ms | ActiveRecord: 1.2ms | 分配: 13194)
但我不知道为什么它不附加
请帮助,我尝试找到相同的问题,但似乎只发生在我身上 =((
附言:对不起,我的英语不好。
<details>
<summary>英文:</summary>
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] = &quot;some thing was wrong&quot;
render :avatar_new, status: :unprocessable_entity
end
end
//erb
&lt;%= form_with model: Current.user, url: avatar_path, method: :post do |form|%&gt;
&lt;%= form.file_field :avatar%&gt;
&lt;%= form.submit &quot;post&quot;%&gt;
&lt;%end%&gt;
and the log
Started POST &quot;/avatar&quot; for ::1 at 2023-06-26 22:23:06 +0700
Processing by UserController#avatar_create as TURBO_STREAM
Parameters: {&quot;authenticity_token&quot;=&gt;&quot;[FILTERED]&quot;, &quot;user&quot;=&gt;{&quot;avatar&quot;=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00007f9b30875500 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20230626-4800-tdl8hi.jpeg&gt;, @content_type=&quot;image/jpeg&quot;, @original_filename=&quot;hello.jpeg&quot;, @headers=&quot;Content-Disposition: form-data; name=\&quot;user[avatar]\&quot;; filename=\&quot;hello.jpeg\&quot;\r\nContent-Type: image/jpeg\r\n&quot;&gt;}, &quot;commit&quot;=&gt;&quot;Tạo ảnh đại diện&quot;}
User Load (0.2ms)  SELECT &quot;users&quot;.* FROM &quot;users&quot; WHERE &quot;users&quot;.&quot;id&quot; = ? LIMIT ?  [[&quot;id&quot;, 4], [&quot;LIMIT&quot;, 1]]
↳ app/helpers/user_helper.rb:4:in `current_user?&#39;
TRANSACTION (0.1ms)  begin transaction
↳ app/controllers/user_controller.rb:29:in `avatar_create&#39;
User Exists? (0.5ms)  SELECT 1 AS one FROM &quot;users&quot; WHERE &quot;users&quot;.&quot;email&quot; = ? AND &quot;users&quot;.&quot;id&quot; != ? LIMIT ?  [[&quot;email&quot;, &quot;tigopro.1703@gmail.com&quot;], [&quot;id&quot;, 4], [&quot;LIMIT&quot;, 1]]
↳ app/controllers/user_controller.rb:29:in `avatar_create&#39;
ActiveStorage::Attachment Load (0.2ms)  SELECT &quot;active_storage_attachments&quot;.* FROM &quot;active_storage_attachments&quot; WHERE &quot;active_storage_attachments&quot;.&quot;record_id&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;record_type&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;name&quot; = ? LIMIT ?  [[&quot;record_id&quot;, 4], [&quot;record_type&quot;, &quot;User&quot;], [&quot;name&quot;, &quot;avatar&quot;], [&quot;LIMIT&quot;, 1]]
↳ app/controllers/user_controller.rb:29:in `avatar_create&#39;
TRANSACTION (0.1ms)  commit transaction
↳ app/controllers/user_controller.rb:29:in `avatar_create&#39;
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&#39;t know why it doesn&#39;t attach
please help, i tried to find the same problem but seems it just happen to me =((
ps: sorry for my bad english
</details>
# 答案1
**得分**: 0
从您分享的日志中看,似乎 `avatar` 在 `user` 散列内。
参数:{"authenticity_token"=&gt;"[FILTERED]", "user"=&gt;{"avatar"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00007f9b30875500 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20230626-4800-tdl8hi.jpeg&gt;, @content_type=&quot;image/jpeg&quot;, @original_filename=&quot;hello.jpeg&quot;, @headers=&quot;Content-Disposition: form-data; name=\&quot;user[avatar]\&quot;; filename=\&quot;hello.jpeg\&quot;\r\nContent-Type: image/jpeg\r\n&quot;&gt;}, "commit"=&gt;"Tạo ảnh đại diện"}
因此,将 `params[:avatar]` 更新为 `params[&#39;user&#39;][&#39;avatar&#39;]` 应该解决您的问题。
<details>
<summary>英文:</summary>
From the logs you shared, it seems `avatar` is inside the `user` hash.
Parameters: {&quot;authenticity_token&quot;=&gt;&quot;[FILTERED]&quot;, &quot;user&quot;=&gt;{&quot;avatar&quot;=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00007f9b30875500 @tempfile=#&lt;Tempfile:/tmp/RackMultipart20230626-4800-tdl8hi.jpeg&gt;, @content_type=&quot;image/jpeg&quot;, @original_filename=&quot;hello.jpeg&quot;, @headers=&quot;Content-Disposition: form-data; name=\&quot;user[avatar]\&quot;; filename=\&quot;hello.jpeg\&quot;\r\nContent-Type: image/jpeg\r\n&quot;&gt;}, &quot;commit&quot;=&gt;&quot;Tạo ảnh đại diện&quot;}
So, updating `params[:avatar]` to `params[&#39;user&#39;][&#39;avatar&#39;]` should solve your issue.
</details>

huangapple
  • 本文由 发表于 2023年6月25日 22:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定