【发布时间】:2022-01-09 18:43:25
【问题描述】:
我正在尝试使用 gem 设计验证枚举,但它没有保存在我的活动记录中。
这是我的模型:
类用户 结束 这是我的控制器: 类 ApplicationController
结束 这是我的观点: 我应该改变什么来保存活动记录中的角色? # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :full_name, presence: true
enum role: %i[admin_user regular_user]
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up) do |user_params|
user_params.permit({ roles:[] }, :full_name,:email, :password, :password_confirmation)
end
end
注册
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email%><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class= "field">
<%= f.label :full_name %>
<%= f.text_field :full_name, autocomplete: "full_name"%>
</div>
<div class="field">
<%=f.label :roles %>
<%= f.select :role, collection: User.roles.keys.to_a %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "new-password" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
【问题讨论】:
-
附带说明 - 在 Ruby 中,按照惯例,任何哈希选项都应该放在参数列表的末尾,您可以去掉括号。虽然在这种情况下可能无关紧要,但如果设计到哪里切换到真正的关键字参数,代码就会停止。
标签: ruby-on-rails ruby devise