【问题标题】:Filter user by role on select rails在选择的轨道上按角色过滤用户
【发布时间】:2016-04-07 16:22:28
【问题描述】:

我的用户具有以下角色:管理员、经理、收银员。用户模型与员工模型的关系。

员工 belongs_to 用户和 用户 has_many Employee

在新形式的员工中,我使用了这种方法

<%= form_for @employee, remote: true, html: {role: "form", multipart: true} do |f| %>
  <div class="form-group">
    <%= f.label :user_id, 'User', class: "exampleInputEmail1" %>
    <%= f.select :user_id, options_for_select(@users.map {|u|[u.name, u.id]}, f.object.user_id), {prompt: "Select User"}, class: "form-control" %>
  </div>

使用上面的方法,我得到了所有的用户。但我只想过滤选择中具有“管理员”角色的用户。如何获得?

编辑

employees_controller

新建和创建操作

  def new
    @employee = Employee.new
    @users = User.all
  end

  def create
    @employee = Employee.new(employee_params)
    @users = User.all
    respond_to do |format|
      if @employee.save
        format.html { redirect_to employees_path, notice: 'Employee was successfully created.' }
        format.json { render :show, status: :created, location: @employee }
        format.js
      else
        format.html { render :new }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end
    end
  end

编辑 2

忘记添加了。关系用户和角色。

class User < ActiveRecord::Base
  has_many :user_roles, :dependent => :destroy
  has_many :roles, :through => :user_roles
end

class Role < ActiveRecord::Base
  has_many :user_roles, :dependent => :destroy
  has_many :users, :through => :user_roles
end

编辑 3

schema.rb

  create_table "roles", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "roles", ["name"], name: "index_roles_on_name", unique: true, using: :btree

  create_table "user_roles", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "role_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  add_index "user_roles", ["role_id"], name: "index_user_roles_on_role_id", using: :btree
  add_index "user_roles", ["user_id"], name: "index_user_roles_on_user_id", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "gender"
    t.string   "username"
    t.string   "email"
    t.string   "salt"
    t.string   "encrypted_password"
    t.string   "auth_token"
    t.string   "password_reset_token"
    t.datetime "password_reset_sent_at"
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["username"], name: "index_users_on_username", unique: true, using: :btree

  add_foreign_key "user_roles", "roles"
  add_foreign_key "user_roles", "users"

  create_table "employees", force: :cascade do |t|
    t.string   "employee_code"
    t.string   "name"
    t.string   "age"
    t.string   "address"
    t.string   "telp"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
    t.integer  "user_id"   
  end

【问题讨论】:

  • 既然,如果我的理解是正确的,你总是只想要这种形式的管理员用户,我建议在控制器中过滤它,然后你正在使用的变量 (@users.map) 将只包含那些管理员用户,你可以发布你的员工控制器吗?
  • 好的。我已经发布了员工控制器
  • 你在哪里定义@users
  • 抱歉,已编辑 :)

标签: ruby-on-rails-4


【解决方案1】:

编辑。

您可以通过以下方式拉动管理员:

@user.roles.where(name: 'Admin')

这假设您的 Role 类已将角色存储在名为“Name”的列中,并且给 Admin 的名称是“Admin” - 如果不是,则将括号内替换为 (your_role_column: 'Your Admin name' )。

【讨论】:

  • 我已经为用户和角色编辑了我的代码。我尝试使用你的方法,这个错误 ERROR: column users.role does not exist
  • 我已经编辑了上面的查询 - 您可以尝试类似的方法。我的原始代码不起作用,因为您没有使用枚举来定义角色,而是使用连接表。您可能想使用我的查询在控制器中定义 @users,然后将该集合传递给 options_for_select 方法。
  • 代码本身将取决于您是否为包含角色“角色”或其他内容的列命名。如果您共享 Role 表的迁移,可能会有所帮助。
  • 我的角色表只有 'name' 属性。我在我的用户模型中使用了“attr_accessor:selected_role”之类的“role_ids”来选择角色名称。
  • 我已经更新了查询。基本上 () 的第一部分是您的 column_name: (给定您的答案,我相信它是“名称:”),第二部分是您所谓的管理员(最有可能是“管理员”,但如果您调用过,请更改它)它是别的东西)。
【解决方案2】:

更好的方法是使用作用域:

在模型中:

scope :admins, ->{ where(name: 'Admin') }

在控制器中:

User.admins

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 2015-08-15
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    相关资源
    最近更新 更多