【问题标题】:How to update specific attributes from restricted model using Best_In_Place and Cancan如何使用 Best_In_Place 和 Cancan 从受限模型更新特定属性
【发布时间】:2014-01-14 05:15:32
【问题描述】:

当同时使用 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 %>

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

【问题讨论】:

    标签: ruby-on-rails cancan best-in-place


    【解决方案1】:

    更新为谈论两件事

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

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

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

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

    【讨论】:

    • 嗯,我有点困惑如何实现这一点:) 简要地看一下它让我觉得我不会再使用 best_in_place 了......还是我弄错了? :) 无论如何,谢谢,我稍后会在家里深入检查一下:)
    猜你喜欢
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多