使用枚举的基于角色的身份验证
roles based authetication using enum
正在构建一个 rails 应用程序,我想在其中拥有两种类型的用户,即卖家和买家。用户可以在注册时选择他们想注册为卖家还是买家。
然后我使用 devise 创建了用户
在 user.rb
中添加了 enum role: [:seller, :buyer]
然后创建迁移以向用户添加角色
rails g migration add_role_to_users
我的迁移看起来像这样:
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :integer
end
end
我使用的是简单的形式,在我的users registration form
中添加了
<%= f.select :role, User.roles %>
在索引页上,我正在尝试这样做:
<% if current_user.seller? %>
<%= link_to 'New Post', new_post_path %>
<% else %>
hello
<% end %>
但是我的用户的角色 return 不知何故为 nil,我也检查了控制台,甚至在那里我的用户 return 的角色为 nil,有人可以帮助我并告诉我我做错了什么。谢谢
你的代码看起来不错,假设你 运行 rake db:migrate
。
在控制台中,任何特定用户的角色 returns 为零,因为您还没有为他们中的任何一个保存角色(例如,当您在控制台中键入 User.last.role
时,您必须得到 nil
) .
但是如果你键入 User.last.update_attributes(role: 1)
,那么在 User.last
的输出中你应该看到他的角色已经被修改,现在是 buyer
。
如果不是这种情况,请分享您收到的错误。
要让它在注册时工作,您必须正确配置您的注册控制器,以允许 Devise 使用新参数。您可以在 中找到所有信息。
简而言之,您可以分三步完成:
1/ 在你的文件中 route.rb
:
devise_for :users, controllers: {
:registrations => "users/registrations" }
2/ 在你的注册控制器中:
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# GET /resource/sign_up
def new
super
end
#then the method called in the before action where you permit the parameters
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :birth_date, :role, :otherkeysyouneed])
end
end
3/ 在您的注册视图中
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.select :role, User.roles %>
你应该被分类。祝你好运,并参考上面链接的 post 和 Devise 文档,如果需要,可以在其中获取更多信息。
正在构建一个 rails 应用程序,我想在其中拥有两种类型的用户,即卖家和买家。用户可以在注册时选择他们想注册为卖家还是买家。
然后我使用 devise 创建了用户
在 user.rb
enum role: [:seller, :buyer]
然后创建迁移以向用户添加角色
rails g migration add_role_to_users
我的迁移看起来像这样:
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :integer
end
end
我使用的是简单的形式,在我的users registration form
中添加了
<%= f.select :role, User.roles %>
在索引页上,我正在尝试这样做:
<% if current_user.seller? %>
<%= link_to 'New Post', new_post_path %>
<% else %>
hello
<% end %>
但是我的用户的角色 return 不知何故为 nil,我也检查了控制台,甚至在那里我的用户 return 的角色为 nil,有人可以帮助我并告诉我我做错了什么。谢谢
你的代码看起来不错,假设你 运行 rake db:migrate
。
在控制台中,任何特定用户的角色 returns 为零,因为您还没有为他们中的任何一个保存角色(例如,当您在控制台中键入 User.last.role
时,您必须得到 nil
) .
但是如果你键入 User.last.update_attributes(role: 1)
,那么在 User.last
的输出中你应该看到他的角色已经被修改,现在是 buyer
。
如果不是这种情况,请分享您收到的错误。
要让它在注册时工作,您必须正确配置您的注册控制器,以允许 Devise 使用新参数。您可以在
1/ 在你的文件中 route.rb
:
devise_for :users, controllers: {
:registrations => "users/registrations" }
2/ 在你的注册控制器中:
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
# GET /resource/sign_up
def new
super
end
#then the method called in the before action where you permit the parameters
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :birth_date, :role, :otherkeysyouneed])
end
end
3/ 在您的注册视图中
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.select :role, User.roles %>
你应该被分类。祝你好运,并参考上面链接的 post 和 Devise 文档,如果需要,可以在其中获取更多信息。