简体   繁体   English

设计管理员模型-如何允许参数(新字段)?

[英]Devise admin model - how to permit parameters (new fields)?

In an app where Devise and Admin model are used I needed to add some fields - they were successfully added. 在使用Devise和Admin模型的应用程序中,我需要添加一些字段-已成功添加它们。 Now I also need to give the user the ability to modify these attributes. 现在,我还需要使用户能够修改这些属性。 When I open the view for modifying these parameters and send the form, the newly added fields (like a phone number, website etc) are not modified. 当我打开用于修改这些参数的视图并发送表格时,新添加的字段(例如电话号码,网站等)不会被修改。

In the terminal output I see it's because these parameters are unpermitted, but how can I permit them? 在终端输出中,我看到是因为不允许使用这些参数,但是如何允许它们呢?

The action where the whole update process is happening is registrations#update : 整个update过程正在进行的动作是registrations#update

def update
    @user = User.find(current_user.id)
    successfully_updated = if needs_password?(@user, params)
      @user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
    else
      params[:user].delete(:current_password)
      @user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
    end

    if successfully_updated
      flash[:notice] = "Your password has been successfully changed."
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to edit_user_registration_path(:status => 'ok')
    else
      render "edit"
    end
  end

Bu this code seems to be for users , not for admins - how can I solve this problem then? 这些代码似乎是针对users ,而不是针对admins -那么我该如何解决这个问题?

Thank you in advance. 先感谢您。

class RegistrationsController < Devise::RegistrationsController
  before_action :configure_permitted_parameters

  # ...

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) << :username
  end
end

In this example we add a :username parameter to the whitelist. 在此示例中,我们将:username参数添加到白名单。

https://github.com/plataformatec/devise#strong-parameters https://github.com/plataformatec/devise#strong-parameters

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

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