【发布时间】:2010-11-03 00:08:01
【问题描述】:
我的应用中有一个权限模型,它将(用户、角色、项目)联系在一起。
我希望学习如何做的是防止用户为他们的项目删除自己...
您能就以下问题给我反馈吗?
class Permission < ActiveRecord::Base
.
.
.
#admin_lock makes sure the user who created the project, is always the admin
before_save :admin_lock
def before_save
#Get the Project Object
project = Find(self.project_id)
if project.creator_id == current_user.id
# SOME HOW ABORT OR SEND BACK Not Allowed?
else
#continue, do nothing
end
end
end
这看起来是正确的方法吗?
另外,我不确定如何做上面的以下两件事:
- 如何中止阻止保存,并返回错误消息?
- 在模型中获取设计 current_user.id,这似乎是不可能的,那么 Rails 大师如何做上述事情?
感谢阅读
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 devise