2016-06-22 3 views
0

레일 응용 프로그램에서 필자는 스키마와 사용자 모델에서 정의한 Admin과 User라는 두 가지 역할 만 가지고 있습니다. 다음은 사용자 테이블 내 schema.rb입니다 :레일 : 적절한 기본 역할로 사용자가 생성되지 않습니다

class User < ActiveRecord::Base 
    #Defining different roles 
    enum role: [:Admin, :User] 
    #Users can only have one scholarship application 
    has_one :applications 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
end 

내 능력 모델 :

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # guest user (not logged in) 
     if user.role = 1 
      can :manage, :all 
     elsif user.role = 2 
      can :manage, Application 
      can :manage, User 
     else 
      can :read, Static_Page 
     end 
    end 
end 

내 사용자 컨트롤러 :

class UsersController < ApplicationController 
    before_action :authenticate_user 
    #Users who are not signed in cannot view users list 
    before_action :set_user, only: [:show, :edit, :update, :destroy] 
    load_and_authorize_resource 

    # GET /users 
    # GET /users.json 
    def index 
    @users = User.all 
    end 

    # GET /users/1 
    # GET /users/1.json 
    def show 
    end 

    # GET /users/new 
    def new 
    @user = User.new 
    end 

    # GET /users/1/edit 
    def edit 
    @user = current_user 
    end 

    # POST /users 
    # POST /users.json 
    def create 
    @user = User.new(user_params) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render :show, status: :created, location: @user } 
     else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /users/1 
    # PATCH/PUT /users/1.json 
    def update 
    respond_to do |format| 
     if @user.update(user_params) 
     format.html { redirect_to @user, notice: 'User was successfully updated.' } 
     format.json { render :show, status: :ok, location: @user } 
     else 
     format.html { render :edit } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /users/1 
    # DELETE /users/1.json 
    def destroy 
    @user.destroy 
    respond_to do |format| 
     format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    #Never trust paramaters from the scary internet, man. 
    def user_params 
     params.require(:user).permit(:first_name, :last_name) 
    end 
end 
첫째

create_table "users", force: :cascade do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.inet  "current_sign_in_ip" 
    t.inet  "last_sign_in_ip" 
    t.integer "role",     default: 2 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 

end 

가 여기 내 사용자 모델입니다

내 의견으로는, nditions는 이와 같은 사용자의 역할에 따라 표시하기 :

<% if user_signed_in? %> 
<h4 class="center"> Welcome <%= current_user.first_name %>!</h4> 
<% end %> 
<% if user_signed_in? && current_user.role = "Admin" %> 

<h4 class="center"> You are a <%= current_user.role %>, you can view all applications, edit them, delete users, and more!!</h4> 
<% elsif user_signed_in? && current_user.role = "User" %> 
<h4 class="center"> Thank you for your interest in our scholarship. As a <%= current_user.role %>, you may create, edit, or delete your scholarship application now that you are signed in.</h4> 

<% else %> 
<h4 class="center"> Welcome to the Philanthropist's Scholarship Application! Please create an account to apply for our scholarship!</h4> 

<% end %> 

나는 내 사용자보기 - 그래서 위 (관리자 용)의 관리자의 CP에 대한 논리와 (사용자 용) 계정 설정보기를 다음에 관리자 CP는 모든 사용자에게 기본 레일을 사용자에게 보여 주지만 계정 설정은 현재 사용자에 대한 사용자 정보 만 표시합니다.

새 사용자를 만들 때 항상 사용자가 관리자이고 새 사용자를 만들고 역할 값이 2 ("사용자")가 아닌 것으로 나타나는 경우에만 문제가 발생합니다. , 그들은 단지 관리이고 아무것도 할 수 있습니다.

제안 사항?

답변

1

올바른 기능을 보려면보기에서 == not =를 사용해야합니다. 엑스트라 귀하의 사용자 테이블에있는 필드는 정수입니다 ...

t.integer "role",     default: 2 

<% if user_signed_in? %> 
<h4 class="center"> Welcome <%= current_user.first_name %>!</h4> 
<% end %> 
<% if user_signed_in? && current_user.role == 1 %> 

<h4 class="center"> You are a <%= current_user.role %>, you can view all applications, edit them, delete users, and more!!</h4> 
<% elsif user_signed_in? && current_user.role == 2 %> 
<h4 class="center"> Thank you for your interest in our scholarship. As a <%= current_user.role %>, you may create, edit, or delete your scholarship application now that you are signed in.</h4> 

<% else %> 
<h4 class="center"> Welcome to the Philanthropist's Scholarship Application! Please create an account to apply for our scholarship!</h4> 

<% end %> 
+0

관리자 또는 사용자 기반으로 인쇄 할 수 있습니다. current_user.role == 1. 당신을 위해 도우미를 작성합니다 게시물을 편집하십시오 - 내가 볼 수있는 순간부터 <% = current_user.role %>, 은 역할 번호에 따라 숫자가됩니다 – Dave

+0

안녕 Dave, 응답 해 주셔서 감사합니다. 나는 지난 밤에 이것을 알아 냈다. 나는 또 다른 요점을 놓쳤다. 나는 열거 형 역할을했다. [: Admin, : User] 그러나 스키마에서 역할을 2-로 설정했지만 나는 두 가지로 설정하지 않았다. admin = 0과 user = 1과 같이 0으로 색인이 생성되었습니다. 그래서, Guest, : User, : Admin으로 설정하고 기본값을 1로 설정했습니다. 감사합니다! –

+0

당신은 환영합니다! 다행히도 그것이 다 처리되면 항상 사악한 느낌입니다! – Dave

관련 문제