2017-02-25 4 views
1

私のビューで役割を選択するのにradiobuttongroupを持っていますが、それをどのように処理するかわかりません。私はすでに宝石を設置していると思いますが、ビューとコントローラーでどのように役割を割り当てるかはわかりません。私は既にコンソールでロールを作成しています。役割を選択する際に悪用がないことを確認したい。たとえば、ユーザーがブラウザから役割名を変更して自分自身(たとえば管理者)に割り当てようとすると、大きな問題になります。登録コントローラのユーザーに役割を割り当て、RolifyとDeviseでそのビューを

registrations_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 

user.rb

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 

ビューの一部登録

<%= 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 %> 

答えて

0

用I rolifyを使用して複雑な機能を管理しようとしても同様の問題がありましたが、私はUserモデル内でロール検証のための非常に良い解決策を得ました。チェックアウトしたい場合に備えてリンクを残します。

Rolify: Validate roles with complex functionality inside the User model

また、私はこれを行う方法を示す別のリンクを残しておきます、registrations_controller内部ユーザ登録後に役割を追加してい:

Adding Role dynamically through Form USing Rolify along with Devise and Cancan

は、この情報がお役に立てば幸い!

関連する問題