【发布时间】:2017-10-11 07:07:25
【问题描述】:
我的rails_admin 应用程序有两个角色,即Teacher 和Student,这样每个用户belongs_to 都有一个角色。我正在使用gem cancancan 来管理角色。
应用/模型
class Role < ApplicationRecord
has_many :users
end
class User < ApplicationRecord
belongs_to :role
has_many :projects
end
class Project < ApplicationRecord
belongs_to :user
end
Project 架构
create_table "projects", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_projects_on_user_id", using: :btree
end
现在,我希望项目列表在 role 为 Teacher 时显示所有数据,但它应该仅在 role 为 Student 时显示 project.user_id == user.id 的那些项目。
也就是说,最终的目的是让roleStudent只能看到他/她自己的项目,从而限制roleStudent看到所有roleTeacher的项目应该可以看到所有的项目。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 cancan rails-admin cancancan