我想获取在我的 ROR 应用程序中创建角色的用户信息,例如 usernaem 或名字。通过将 role_users 作为连接表,用户和角色之间存在多对多关联。我能够创建角色并保存它们。但我不知道如何获得创建角色的用户。例如,如果我是管理员,我可以在应用程序中创建新角色。在创建角色时,我需要获取正在创建该角色的用户roles_controller.rb
。
角色控制器.rb
class RolesController < ApplicationController
before_filter :authorize_admin!
def index
@roles = Role.all
end
def new
@role = Role.new
end
def create
@role = Role.new(params[:role])
# @article.user_id = current_user.id
@role_user.user_id = current_user.id
if @role.save
flash[:success] = "role created!"
redirect_to roles_path(@role)
else
render 'new'
end
end
def show
@role = Role.find(params[:id])
end
def edit
@role = Role.find(params[:id])
end
def update
@role = Role.find(params[:id])
if @role.update_attributes(params[:role])
flash.notice = "Role #{@role.name} has been updated"
redirect_to role_path(@role)
else
render 'edit'
end
end
def destroy
@role = Role.find(params[:id])
@role.destroy
redirect_to action: 'index'
end
end
users_controller.rb
class Admin::UsersController < Admin::BaseController
before_filter :find_user, :only => [:show, :edit, :update, :destroy]
def index
@users = User.all( :order => :email )
@roles = Role.all
end
def show
end
def new
@user = User.new
end
def create
is_admin = params[:user].delete(:is_admin) == "1"
@user = User.new(params[:user])
@user.save
@user_role = RoleUser.new({:user_id => @user.id, :role_id => params[:role_id]})
@user_role.role_id = params[:role_id]
@user_role.save
@user.is_admin = is_admin
if @user.save
flash[:notice] = "User has been created."
redirect_to admin_users_path
else
flash[:alert] = "User has not been created."
render :action => :new
end
end
def edit
end
def update
if params[:user][:password].empty?
params[:user].delete(:password)
end
set_admin
if @user.update_attributes(params[:user])
flash[:notice] = "User has been updated."
redirect_to admin_users_path
else
flash[:alert] = "User has not been updated."
render :action => :new
end
end
def destroy
if @user == current_user
flash[:alert] = "You cannot delete yourself!"
else
@user.destroy
flash[:notice] = "User has been deleted."
end
redirect_to admin_users_path
end
private
def find_user
@user = User.find(params[:id])
end
def set_admin
is_admin = params[:user].delete(:is_admin) == "1"
@user.is_admin = true
end
end
用户.rb
class User < ActiveRecord::Base
has_many :roles, through: :role_users
has_many :role_users
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :password, :password_confirmation,
:remember_me, :first_name, :last_name, :is_admin, :contact_no, :birth_date,
:joining_date, :is_active, :is_hr, :is_manager, :user_code, :designation
# attr_accessible :title, :body
end
角色用户.rb
class RoleUser < ActiveRecord::Base
belongs_to :user
belongs_to :role
attr_accessible :role_id, :user_id
end
角色.rb
class Role < ActiveRecord::Base
attr_accessible :name
has_many :users, through: :role_users
has_many :role_users
end
ActiveRecord::Schema.define(:version => 20130601093644) 做
create_table "role_users", :force => true do |t|
t.integer "role_id"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
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
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "is_admin"
t.string "username"
t.string "first_name"
t.string "last_name"
t.string "contact_no"
t.date "birth_date"
t.boolean "is_active"
t.date "joining_date"
t.string "avatar_url"
t.boolean "is_hr"
t.boolean "is_manager"
t.string "designation"
t.string "user_code"
t.string "user_role"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true