英文:
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] = "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", "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)
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
</details>
# 答案1
**得分**: 0
从您分享的日志中看,似乎 `avatar` 在 `user` 散列内。
参数:{"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']` 应该解决您的问题。
<details>
<summary>英文:</summary>
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.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论