0
在我的Rails應用程序中,我只有兩個角色,Admin和User,我在我的架構和用戶模型中定義了這兩個角色。這裏是我的schema.rb與用戶表:Rails:未使用正確的默認角色創建用戶
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
首先,這裏是我的用戶模型:
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
在我看來,我加了一些co基於用戶的角色要顯示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上述邏輯(管理員)和帳戶設置(對於用戶)視圖中的admin CP會向用戶顯示所有用戶(如默認導軌腳手架),而帳戶設置僅顯示當前用戶的用戶信息。
唯一的問題是,當我創建一個新用戶時,它總是說用戶是管理員,我創建一個新用戶,並且角色值似乎不等於2(「用戶」) ,他們只是一個管理員,可以做任何事情。
有什麼建議嗎?
如果current_user.role == 1,您可以使用助手方法來打印管理員或用戶。將爲您整合助手並編輯帖子 - 從目前我可以看到 你是一個<%= current_user.role%>, 將根據他們的角色編號 – Dave
嗨戴夫,謝謝你的迴應。我設法弄清楚了昨晚 - 我也錯過了另一個關鍵點,我有枚舉角色:[:Admin,:User],但是我在模式中的角色設置爲2,但我沒有設置爲2,因爲它是0索引,如管理員= 0和用戶是1.所以,我做到了:來賓,:用戶,:管理和默認值設置爲1.謝謝! –
不客氣!很高興你把它整理好了,當它一切正常時,它總是一種邪惡的感覺! – Dave