2017-02-25 12 views
1

radiobuttongroup에 내 뷰에서 역할을 선택하고 싶지만이를 처리하는 방법이 확실하지 않습니다. 나는 이미 설정 한 보석을 사용하고 기분 전환 할 수 있지만 뷰와 컨트롤러에서 역할을 할당하는 방법을 모르겠습니다. 이미 콘솔에서 역할을 만들었습니다. 역할을 선택할 때 악용 사례가 없는지 확인하고 싶습니다. 예를 들어 사용자가 브라우저에서 역할 이름을 변경하여 해당 역할 이름 (예 : admin)을 할당하려고하면 큰 문제가됩니다.등록 컨트롤러에서 사용자에게 역할을 할당하고 롤리 그 및 장치를 사용하여 뷰를 할당하는 방법

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 등록

<%= 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보기의

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 

부 rolify를 사용하여 복잡한 기능을 관리하려는 비슷한 문제가 있었지만 사용자 모델 내부에서 역할 검증을위한 훌륭한 솔루션을 얻었습니다. 체크 아웃하려는 경우 링크를 남깁니다.

Rolify: Validate roles with complex functionality inside the User model

또한, 나는이 registrations_controller 내부 사용자 등록 후 역할을 추가하고, 나는이 작업을 수행하는 방법을 보여주는 다른 링크를 떠날거야 :이 도움이

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

희망을!