0

ROR アプリケーションでロールを作成した usernaem や名などのユーザー情報を取得したいと考えています。role_users を結合テーブルとして持つことにより、ユーザーとロールの間に多対多の関連付けがあります。ロールをクレートして保存できます。しかし、ロールを作成するユーザーを取得する方法がわかりません。たとえば、私が管理者の場合、アプリケーションで新しいロールを作成できます。ロールの作成中に、そのロールを作成しているユーザーを取得する必要があります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(:バージョン => 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
4

3 に答える 3

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])

しかし、私はあなたを完全にフォローしているとは思いません。これはあなたが求めているものですか?

于 2013-06-02T20:58:48.217 に答える