gpt4 book ai didi

ruby-on-rails - Rails file_field 标记仅将字符串文件名传递给 Controller

转载 作者:行者123 更新时间:2023-12-04 03:49:36 25 4
gpt4 key购买 nike

我的 file_field 标签只是将文件名而不是文件本身传递给 Controller ​​。

在我的用户模型中:

mount_uploader :avatar, AvatarUploader
validate :avatar_size
...
def avatar_size
errors.add :avatar, "should be less than 5MB" if avatar.size > 5.megabytes
end

头像上传者:
class AvatarUploader < CarrierWave::Uploader::Base

include CarrierWave::MiniMagick
process resize_to_limit: [50, 50]

if Rails.env.production?
storage :fog
else
storage :file
end

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def extension_white_list
%w(jpg, jpeg, gif, png)
end

end

和用户 Controller :
before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy]
skip_before_action :verify_authenticity_token, only: [:create]
...
def update
@user = User.find(params[:id])
if @user.update_attributes user_params
flash[:success] = "Profile updated"
redirect_to user_path @user
else
render 'edit'
end
end
...
def user_params
params.require(:user).permit :name, :email, :password, :password_confirmation, :avatar
end

def correct_user
@user = User.find params[:id]
redirect_to root_path unless @user == current_user
end

def admin_user
redirect_to root_path unless current_user.admin?
end

View 中的表格:
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>

<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>

<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>

<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>

<% if @user.avatar? %>
<%= image_tag @user.avatar.url, size: "50x50" %>
<% else %>
<%= image_tag "stock_ava.png", size: "50x50" %>
<% end %>

<%= f.file_field :avatar, accept: 'image/jpeg,image/gif,image/png' %>

<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>

在控制台中,我可以看到属性是“FinnJake.png”,而不是相应的 ActionDispatch。
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"h9tD/g2M1zJ5L4cnysIQwHCWxDdhhhJR1I9a9+FAWeT9aTXPte16Uyn8GvI4w23klfvYtTaLCDrGVonHfmFUag==", "user"=>{"name"=>"Example User", "email"=>"example@user.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>"FinnJake.png"}, "commit"=>"Save Changes", "id"=>"1"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.1ms) begin transaction
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND ("users"."id" != ?) LIMIT ? [["email", "bexample@user.com"], ["id", 1], ["LIMIT", 1]]
SQL (1.1ms) UPDATE "users" SET "avatar" = ?, "updated_at" = ? WHERE "users"."id" = ? [["avatar", nil], ["updated_at", "2017-10-02 14:50:41.226691"], ["id", 1]]
(142.3ms) commit transaction
Redirected to https://rubyonrails-xxx.c9users.io/users/1
Completed 302 Found in 155ms (ActiveRecord: 144.1ms)

我对 Rails 很陌生,但我不明白为什么 file_field 没有上传文件。我可以在浏览器中看到我最终得到了这个标签:
<input accept="image/jpeg,image/gif,image/png" type="file" name="user[avatar]" id="user_avatar">

这对我来说是正确的。我似乎也无法通过 rails 控制台手动更新该列,但我没有收到任何错误:
2.4.0 :002 > user = User.first
User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, name: "Example User", email: "example@user.com", created_at: "2017-10-02 14:32:22", updated_at: "2017-10-02 16:35:55", password_digest: "$2a$10$sww7gItks0mYz6xz8zV.QOIjr6to/o5Jytjmuo0wxny...", remember_digest: nil, admin: true, activation_digest: "$2a$10$W396r7A8aZaii3DC7b3W7u70etSmXh3MORMIoWtcSHo...", activated_at: "2017-10-02 14:32:22", reset_digest: nil, reset_sent_at: nil, avatar: nil>
2.4.0 :003 > user.update avatar: "String"
(0.2ms) begin transaction
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND ("users"."id" != ?) LIMIT ? [["email", "example@user.com"], ["id", 1], ["LIMIT", 1]]
SQL (2.4ms) UPDATE "users" SET "updated_at" = ?, "avatar" = ? WHERE "users"."id" = ? [["updated_at", "2017-10-02 17:27:06.097363"], ["avatar", nil], ["id", 1]]
(11.4ms) commit transaction
=> true

预先感谢您提供的任何建议。

最佳答案

您应该使用多部分形式,例如:

form_for(@user, html: { multipart: true }) do ...

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-file_field

关于ruby-on-rails - Rails file_field 标记仅将字符串文件名传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46530610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com