简体   繁体   English

在设计用户模型上添加额外的字段

[英]Add extra fields on devise user model

I am trying to create new user with custom fields.我正在尝试使用自定义字段创建新用户。 I have created a migration and successfully added new fields to the database table.我创建了一个迁移并成功地向数据库表中添加了新字段。

add_column :users, :status, :string

On my view在我看来

<%= form_for @user do |f| %>
<div class="form-group">
  <label for="status">Status:</label>
  <%= f.collection_select :status, {'1' => :Live, '2' => :Restricted }, :first, :last, {prompt: "Status"}, {class: "form-control"}%>
 </div>

On my controller在我的控制器上

def user_params
params.require(:user).permit(:email, :status)

end结尾

And when I am trying to save a new user I am getting the following error message.当我尝试保存新用户时,我收到以下错误消息。

:status=>["is not included in the list"]

You have to configure the permitted parameters for the sign_up , sign_in and account_update Devise actions in your application_controller .你必须配置允许的参数sign_upsign_inaccount_update在你设计的动作application_controller Something like this:像这样的东西:

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :status
    devise_parameter_sanitizer.for(:sign_in) << :status
    devise_parameter_sanitizer.for(:account_update) << :status
  end
end

See this for reference and more information on this.请参阅this以供参考和有关此的更多信息。

Also, you need to comply with the validation that you may have for the status field of your User model.此外,您需要遵守您可能对User模型的status字段进行的验证。

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

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