我想獲取用戶信息,如usernaem或名字在我的ROR應用程序中創建角色。通過將role_users作爲連接表,用戶和角色之間存在多對多關聯。我能夠角色扮演並保存角色。但我不知道如何獲得創建角色的用戶。例如,如果我是Admin,我可以在應用程序中創建新的角色。在創建角色時,我需要獲得在roles_controller.rb
中創建角色的用戶。如何獲得在rails上創建角色的用戶
roles_controller.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
user.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
role_user.rb
class RoleUser < ActiveRecord::Base
belongs_to :user
belongs_to :role
attr_accessible :role_id, :user_id
end
role.rb
class Role < ActiveRecord::Base
attr_accessible :name
has_many :users, through: :role_users
has_many :role_users
end
的ActiveRecord :: Schema.define(:版本=> 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
喜Narfanator,感謝您的回覆。基本上我想獲得在應用程序中創建角色的用戶。例如,如果您是管理員,則可以創建新角色。然後,我想獲取有關您的信息(用戶名)。爲此,我在roles_controller.rb中做了@ role_user.user_id = current_user.id,我不知道如何從這裏獲取用戶信息,因爲user_id保存在role_user表中。 –