设计configure_permitted_pa​​rameters不允许:avatar变量

时间:2019-06-18 09:20:55

标签: ruby-on-rails devise

我正在使用devise 4.6.1和rails 5.2.2

我试图使用:avatar允许application controller中的configure_permitted_parameters,但是,当我尝试通过account_update表单更新用户时,出现错误:< / p>

Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C3zs4PT5Ui5MCVBr6Q9S05TJi2UThwGFQ+s0kTKqb+v4JHeTrl0sfeB5p+owee3DnfJ86BkgPcfNpqgWsdPqcg==", "user"=>{"username"=>"testuser2", "email"=>"testuser2@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f4ced47a108 @tempfile=#<Tempfile:/tmp/RackMultipart20190618-18771-b0w9jf.png>, @original_filename="category-icon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"category-icon.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ /home/ec2-user/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ /home/ec2-user/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Unpermitted parameter: :avatar
   (0.1ms)  begin transaction
  ↳ /home/ec2-user/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
   (0.0ms)  commit transaction
  ↳ /home/ec2-user/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Unpermitted parameter: :avatar
Unpermitted parameter: :avatar
Unpermitted parameter: :avatar

我尝试更改

before_action :configure_permitted_parameters, if: :devise_controller?

before_action :configure_permitted_parameters

但是,当我这样做时,出现以下错误:

resource_class的未定义局部变量或方法DiscussionsController:0x00007f4cee0855d8

application_controller:

before_action :configure_permitted_parameters, if: :devise_controller?

protected

   def configure_permitted_parameters
     devise_parameter_sanitizer.permit(:account_update, keys: [:avatar])
   end

2 个答案:

答案 0 :(得分:0)

也许试试这个:

devise_parameter_sanitizer.for(:account_update).push(:avatar)

我认为这确实是最好的方法:

before_action :configure_permitted_parameters, if: :devise_controller?

答案 1 :(得分:0)

好像您没有重定向到正确的控制器。 您是否覆盖控制器? 如果没有,则应通过 Devise::RegistrationsController#update as HTML 但对于您而言,它是由RegistrationsController#update as HTML处理 可能这就是原因。