我只想在用户是管理员时显示注释。官方生成的initilizer有以下几行:
# Whether or not to enable footnotes
f.enabled = Rails.env.development?
# You can also use a lambda / proc to conditionally toggle footnotes
# Example :
# f.enabled = -> { User.current.admin? }
# Beware of thread-safety though, Footnotes.enabled is NOT thread safe
# and should not be modified anywhere else.
但我不能做得比这更好:
f.enabled = true
f.before {|controller, filter| filter.notes = controller.current_user.try(:admin?) ? [:controller, :view, :layout, :partials, :stylesheets, :javascripts, :assigns, :session, :cookies, :params, :filters, :routes, :env, :queries, :log] : [] }
它有效,但它正确/安全吗?我真正想要的是通过清空或不清空注释列表来模拟 rails-footnots 启用。如何使用官方的 lambda 表示法?User.current.admin?
对我来说没有意义,因为模型不应该有权访问会话