1
我想要2種用戶,老師和學生。現在的問題是,即使我在用戶控制器中爲教師和學生設置了角色,但我的表單顯示選項顯示了VIP和Admin。這是一個設計的東西?我是否需要向我的遷移中添加列?這是一個設計的東西? (註冊後爲用戶設置角色)
以下是註冊表格的一部分。省略意味着提前跳過。
<div class="authform">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
...
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<div>
<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
</div>
<%= f.submit 'Sign Up', :class => 'button right' %>
<% end %>
</div>
這裏是我的用戶控制器
class UsersController < ApplicationController
# before_action :authenticate_user!
before_action :logged_in_user, only: [:edit, :update]
before_action :correct_user, only: [:edit, :update]
after_action :verify_authorized
# before_filter :check_role
enum role: [:student, :teacher]
def show
@user = User.find(params[:id])
authorize @user
@posts = @user.posts
end
def new
@user = User.new
authorize @user
end
def update
@user = User.find(params[:id])
authorize @user
if @user.update_attributes(secure_params)
redirect_to users_path, :notice => "User updated."
else
redirect_to users_path, :alert => "Unable to update user."
end
end
def destroy
user = User.find(params[:id])
# authorize @user
user.destroy
redirect_to users_path, :notice => "User deleted."
end
def create_user_type
end
private
def secure_params
params.require(:user).permit(:role)
end
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless @user == current_user
end
end
所有的老師和學生都有自己的遷移,控制器和輔助文件。這裏是用戶遷移。
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :integer
end
end
對不起,這是一個很長的帖子,我只是有點困惑,爲什麼它沒有學生和教師作爲選項,而是顯示 the following
太謝謝你了!
角色在Devise中不是默認設置。您的代碼不會在用戶表中指明角色屬性。那是怎麼增加的。您可以爲角色創建自己的列(標準是布爾型的管理員屬性),或者您可以使用授權寶石(如權威人士)。 Devise是爲身份驗證而建立的,你正在尋找授權(首先要學習這種差異是一種痛苦) – MageeWorld
這是分配角色的一種不好的方式,試着在STI和CanCan上使用某些東西,可能是 – Abhinay