如何让用户在ruby上创建角色

时间:2013-06-02 20:23:45

标签: ruby-on-rails ruby

我希望获得用户信息,例如在我的ROR应用程序中创建角色的usernaem或名字。通过将role_users作为连接表,用户和角色之间存在多对多的关联。我可以创建角色并保存它们。但我不知道如何让用户创造角色。例如,如果我是Admin,我可以在应用程序中创建新角色。在创建角色时,我需要让用户在roles_controller.rb中创建该角色。

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(:version =&gt; 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

3 个答案:

答案 0 :(得分:0)

通过以下表格传递user_id作为参数:

#_form.html.haml
  %input{:name => "user_id, :value => current_user.id, :type => "hidden"}

#controller:
  creating_user = User.find(params[:role][:user_id])

但我不认为我完全跟着你。这就是你要追求的吗?

答案 1 :(得分:0)

如果你想从.role获取用户对象,请执行@ role.user。它将执行sql查询,它将连接所有3个表并返回给你一个用户

答案 2 :(得分:0)

根据我的理解,您需要在角色模型中再添加一列,即user_id。在他们之间做出适当的关联。用户可以拥有多个角色,角色属于User。 在创建新角色时,您还需要传递user_id或使用关联,这将使您免于明确传递user_id值。 例如: - @ current_user.roles.new(PARAMS [:NEW_ROLE])

当你将使用role_user对象时,你可以通过模型之间的正确关联来获取角色所有者数据,即User,Role,RoleUser。

例如: - @ role_user.role.user