我正试图允许用户从form_for上传带有载波波宝石的图像,但出现此错误:
不允许的参数::avatar
(请参阅下面的完整堆栈跟踪)
我使用的是devise gem,因此我必须按照文档中的说明在application_controller中添加强参数,但是它不起作用。
application_controller.rb
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
attributes = [:name, :email, :avatar]
devise_parameter_sanitizer.permit(:sign_up, keys: attributes)
devise_parameter_sanitizer.permit(:account_update, keys: attributes)
end
end
users_controller.rb
class UsersController < ApplicationController
before_action :ensure_admin, :except => :show
def index
@users = User.paginate(:page => params[:page], :per_page => 8)
end
def show
@user = User.find_by_name(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(user_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
user = User.find(params[:id])
user.destroy
redirect_to users_path, :notice => "User deleted."
end
private
def ensure_admin
if(current_user.role == 'admin')
return
end
redirect_to root_path, :alert => "Access denied."
end
def user_params
params.require(:user).permit(:avatar, :role, :email, :name, :password)
end
end
我的观点:
<div class="field">
<%= f.label :avatar %>
<%= f.file_field :avatar, class: 'form-control' %>
</div>
</div>
<div class="field">
<%= f.label I18n.translate('user.current_password') %> <i><%= I18n.translate('user.needed_to_confirm') %></i>
<%= f.password_field :current_password, placeholder: I18n.translate('user.current_password'), autocomplete: "current-password", class: 'form-control' %>
</div>
<div class="actions">
<%= f.submit I18n.translate('control.update'), class: 'form-control btn sign-up-button' %>
</div>
用户模型
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
mount_uploader :avatar, AvatarUploader
end
schema.rb
ActiveRecord::Schema.define(version: 2019_07_30_090404) do
enable_extension "plpgsql"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "provider"
t.string "uid"
t.string "name"
t.text "image"
t.string "role"
t.string "avatar"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
完整堆栈跟踪:
在2019-08-02 11:23:40为127.0.0.1开始PUT“ / users?locale = en” +0200通过RegistrationsController#update作为HTML参数进行处理:{“ utf8” =>“✓”, “ authenticity_token” =>“ k4pnLdkhc9sFOLax51vkSv0SA8SfnL7Niw3gfo60nmLfBS1I7FD25P54Q / 1qzF4aHIFiBtvq5TzLKk1 / LwZHzw ==”, “用户” => {“名称” =>“ Clovis5”,“电子邮件” =>“ clovi@clovi.com”, “ avatar” =>#, @ original_filename =“ IMG_20190712_150908_155__01.jpg”, @ content_type =“ image / jpeg”,@ headers =“ Content-Disposition:form-data; 名称= \“用户[头像] \”; filename = \“ IMG_20190712_150908_155__01.jpg \” \ r \ n内容类型: image / jpeg \ r \ n“>,”密码“ =>” [已过滤]“, “ password_confirmation” =>“ [已过滤]”, “ current_password” =>“ [FILTERED]”},“ commit” =>“ Update”,“ locale” =>“ en”} 用户负载(0.4毫秒)选择“用户”。*从“用户”到“用户”。“ id” = $ 1 ORDER BY“ users”。“ id” ASC LIMIT $ 2 [[“ id”,15],[“ LIMIT”,1]]↳ /Users/clovisgenevard/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 用户负载(0.3毫秒)选择“用户”。*从“用户”位置“用户”。“ id” = $ 1 LIMIT $ 2 [[“ id”,15],[“ LIMIT”,1]]↳ /Users/clovisgenevard/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 不允许的参数::avatar(0.1ms)BEGIN↳ /Users/clovisgenevard/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 (0.1ms)提交↳ /Users/clovisgenevard/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 不允许的参数::avatar不允许的参数::avatar 不允许的参数::avatar重定向到 http://localhost:3000/?locale=en完成了137毫秒内找到302 (ActiveRecord:1.0毫秒)