绕过标记为公共的特定记录的设计授权的最佳方法是什么

我正在使用Rails 3.2项目中的设计和cancan。 我有一个带有布尔标志public事件模型。 如果事件被标记为public => true,那么我希望任何人登录或不能访问该记录

GET /events/:id 

如果它被标记为public => false,那么一系列康康技能将决定授权和访问上述资源。

实现这一目标的最佳模式是什么?

你可以跳过authenticate_user来做到这一点! 如果你有这个args

  skip_before_filter :authenticate_user!, :only => :show, :if => lambda { if params[:id] @event = Event.find(params[:id]) @event and @event.public? else false end } 

不,不要跳过身份validation。 您想跳过授权 。 更好的解决方案是使用public => true显式授权事件。

在你的康康舞能力课程中:

 can :read, Event do |e| some_other_authorization_boolean || e.public? end 

这种能力将给予所有用户; 即使是那些没有登录的人。