0

当同时使用 Cancan 和 Best_In_Place 时,如何说“老师”仅更新模型的某些属性(例如模型“书”),而他/她无法更新该模型的其他属性。我知道 Cancan 尚不支持属性授权,因为它仍在 2.0 中开发,所以我想到的第一件事是使用这样的东西:

cannot :all, Book
if is_teacher?
    can :update_certain_attribute, Book
end

但由于我想使用 Best_In_Place,我的初步研究告诉我,我只能使用属性进行 Best_In_Place 更新,如下所示

<%= best_in_place @book, :certain_attribute, :type => :input %>

而不是方法:

<%= best_in_place @book, :update_certain_attribute, :type => :input %>

处理这个问题的最佳方法是什么?谢谢 :)

4

1 回答 1

0

更新为谈论两件事

这将确保变量是属性可访问的,因此如果更新使用 best_in_place,模型/控制器将不允许实际更新变量

为此,我认为您需要查看可访问的动态属性。瑞安在这里有一个很好的演员http://railscasts.com/episodes/237-dynamic-attr-accessible

要在 best_in_place 上使用 cancan 有条件,您可以使用以下代码

同样,这对特定属性没有帮助,但与可访问的动态属性结合使用将确保变量不会被更改

于 2014-01-14T05:37:15.583 回答