简体   繁体   English

如何通过设计允许新参数进行主动存储?

[英]How to permit a new param with devise for active storage?

I'm on the latest version of devise 4.6.1, with rails 5.2. 我使用的是带有Rails 5.2的最新版本的devise 4.6.1。 Whitelisting params in the application controller doesn't work for some reason. 由于某些原因,在应用程序控制器中将参数列入白名单无效。 This code is taken directly from the documentation, I'm at a loss as to why this isn't working. 这段代码直接从文档中获取,我不知道为什么它不起作用。

Application_controller: Application_controller:

class ApplicationController < ActionController::Base
 # protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_action :channel_variable

    def channel_variable
    @channels = Channel.all.order('created_at desc')
    end

  protected

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

    devise_parameter_sanitizer.permit(:sign_in, keys: [:avatar])

    devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar])
  end
end

Console: 安慰:

Unpermitted parameter: :avatar

edit.html.erb: edit.html.erb:

      <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
        <%= f.error_notification %>

          <div class="field">
            <div class="control">
              <%= f.label :avatar %>
              <%= f.file_field :avatar %>
            </div>
          </div>
<% end %>

I've found it best to create a separate route and UsersController to modify User attributes outside of authentication, including Active Storage attachments. 我发现最好创建一个单独的路由,并使用UsersController修改身份验证之外的User属性,包括Active Storage附件。 You'd want to create a users#update method and whitelist your avatar param there. 您想要创建一个users#update方法,并将您的avatar参数列入白名单。 This is a bit dated but the accepted answer is the basically the approach you'd take: 这有点过时,但可接受的答案基本上是您要采用的方法:

Rails: Devise: How can I edit user information? Rails:设计:如何编辑用户信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM