Ruby on rails 如何在注册控制器中为用户分配角色及其视图

Ruby on rails 如何在注册控制器中为用户分配角色及其视图,ruby-on-rails,ruby,ruby-on-rails-3,devise,rolify,Ruby On Rails,Ruby,Ruby On Rails 3,Devise,Rolify,我想让radiobuttongroup在我的视图中选择角色,但我不确定如何处理它。我已经设置了gems rolify和Desive,但我不知道如何在view和controller中分配角色。我已经在控制台中创建了角色。我想确保在选择角色时没有任何漏洞。例如,如果此人试图从浏览器中更改角色名称并将其分配给自己(例如管理员),这将是一个大问题 注册\u controller.rb class Users::RegistrationsController < Devise::Registrati

我想让radiobuttongroup在我的视图中选择角色,但我不确定如何处理它。我已经设置了gems rolify和Desive,但我不知道如何在view和controller中分配角色。我已经在控制台中创建了角色。我想确保在选择角色时没有任何漏洞。例如,如果此人试图从浏览器中更改角色名称并将其分配给自己(例如管理员),这将是一个大问题

注册\u controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]

  def create
    super
  end

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname, :lastname, :email, :terms_of_service])
  end
class User < ApplicationRecord
  rolify

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
  validates :terms_of_service, :allow_nil => false, :acceptance => true

end
类用户::注册控制器<设计::注册控制器
在\u操作之前:配置\u注册\u参数,仅:[:创建]
def创建
超级的
结束
def配置\注册\参数
设计消毒剂。许可证(:注册,密钥:[:firstname,:lastname,:email,:服务条款])
结束
user.rb

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]

  def create
    super
  end

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstname, :lastname, :email, :terms_of_service])
  end
class User < ApplicationRecord
  rolify

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable
  validates :terms_of_service, :allow_nil => false, :acceptance => true

end
class用户false,:接受=>true
结束
部分注册视图

<%= form_for(resource, as: resource_name, :role => "form", url: registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>
  <div class="form-group">
    <%= f.label t('label.user.form_content.firstname') %><br/>
    <%= f.text_field :firstname, autofocus: true, :class => "form-control text-center" %>
  </div>
  <div class="form-group">
    <%= f.label t('label.user.form_content.lastname') %><br/>
    <%= f.text_field :lastname, :class => "form-control text-center" %>
  </div>
  <div class="form-group">
    <%= f.label t('label.user.form_content.email') %><br/>
    <%= f.email_field :email, :class => "form-control text-center" %>
  </div>
  <div class="row">
    <div class="col-md-6">
      <div class="form-group">
        <%= f.label t('label.user.form_content.password') %>
        <% if @minimum_password_length %>
            <em>(<%= @minimum_password_length %> characters minimum)</em>
        <% end %><br/>
        <%= f.password_field :password, autocomplete: "off", :class => "form-control text-center" %>
      </div>
    </div>
    <div class="col-md-6">
      <div class="form-group">
        <%= f.label t('label.user.form_content.password_confirmation') %>
        <% if @minimum_password_length %>
            <em>(Must be same with password)</em><br/>
        <% end %><br/>
        <%= f.password_field :password_confirmation, autocomplete: "off", :class => "form-control text-center" %>
      </div>
    </div>
  </div>
<% end %>
“表单”,url:registration_path(resource_name))do | f |%>

“表单控制文本中心”%>
“表单控制文本中心”%>
“表单控制文本中心”%> (最少字符)
“表单控制文本中心”%> (必须与密码相同)

“表单控制文本中心”%>
我在尝试使用rolify管理复杂功能时遇到了类似的问题,我在用户模型中找到了一个很好的角色验证解决方案,我会留下一个链接,以备您查看

此外,我在registrations\u controller中添加用户注册后的角色,我将留下另一个链接,展示如何执行此操作:

希望这有帮助