Ruby on rails 如何获取在RubyonRails中创建角色的用户

Ruby on rails 如何获取在RubyonRails中创建角色的用户,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想获得用户信息,如usernaem或在我的ROR应用程序中创建角色的名字。通过将role_用户作为联接表,用户和角色之间存在多对多关联。我能够包装角色并保存它们。但我不知道如何找到创建角色的用户。例如,如果我是管理员,我可以在应用程序中创建新角色。创建角色时,我需要在roles\u controller.rb中找到创建该角色的用户 角色\u controller.rb class RolesController < ApplicationController before_filte

我想获得用户信息,如usernaem或在我的ROR应用程序中创建角色的名字。通过将role_用户作为联接表,用户和角色之间存在多对多关联。我能够包装角色并保存它们。但我不知道如何找到创建角色的用户。例如,如果我是管理员,我可以在应用程序中创建新角色。创建角色时,我需要在
roles\u controller.rb
中找到创建该角色的用户

角色\u 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
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
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
class RoleUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
  attr_accessible :role_id, :user_id
end
class Role < ActiveRecord::Base
  attr_accessible :name
  has_many :users,  through: :role_users
  has_many :role_users

end

通过以下表单将用户id作为参数传递:

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

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

但我不认为我完全了解你。这就是您想要的吗?

如果您想从.role获取用户对象,只需执行@role.user即可。它将执行sql查询,该查询将连接所有3个表,并向您返回一个用户

,根据我的理解,您需要在角色模型(即用户id)中再添加一列。在它们之间建立适当的关联,如。用户可以有多个角色,角色属于用户。 创建新角色时,您还需要传递用户id,或者使用关联,这样可以避免显式传递用户id值。 例:- @当前用户角色新建(参数[:新建角色])

当您创建角色\用户对象时,您可以获取角色所有者数据,这些数据在三个模型(即用户、角色、角色用户)之间具有适当的关联

例:-
@role\u user.role.user

你好,谢谢你的回复。基本上,我希望获得在应用程序中创建角色的用户。例如,如果您是管理员,则可以创建新角色。然后我想获取关于你的信息(用户名)。为此,我在roles\u controller.rb中设置了@role\u user.user\u id=current\u user.id,我不知道如何从这里获取用户信息,因为用户id保存在role\u user表中。
#_form.html.haml
  %input{:name => "user_id, :value => current_user.id, :type => "hidden"}

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