2017-02-25 2 views
1

Ich möchte radiobuttongroup haben, um Rolle in meiner Ansicht zu wählen, aber ich bin nicht sicher, wie man damit umgeht. Ich habe bereits die Edelsteine ​​rolify und devise aber ich bin nicht sicher, wie Rollen in View und Controller zuweisen. Ich habe bereits Rollen in der Konsole erstellt. Ich möchte sicherstellen, dass es bei der Auswahl der Rolle keine Exploits gibt. Zum Beispiel, wenn die Person versucht, den Rollennamen vom Browser zu ändern und diesen sich selbst zuzuweisen (z. B. admin), wäre das ein großes Problem.Wie man dem Benutzer in Registration Controller und seiner Ansicht mit Rolify und Devise eine Rolle zuweisen

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 

Teil Ansicht zur Anmeldung

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

Antwort

0

I hatte ein ähnliches Problem versucht, komplexe Funktionalität mit Rolify zu verwalten, ich habe eine ziemlich gute Lösung für die Rollen Validierung innerhalb des Benutzermodells, ich werde einen Link hinterlassen, falls Sie es auschecken möchten.

Rolify: Validate roles with complex functionality inside the User model

Auch bin ich die Rollen nach Benutzer-Registrierung innerhalb des registrations_controller hinzufügen, werde ich einen anderen Link zeigt verlassen, wie dies zu tun: das hilft

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

Hoffnung!

Verwandte Themen