【问题标题】:If questions with Devise and Rails?如果对 Devise 和 Rails 有疑问?
【发布时间】:2012-06-25 13:26:20
【问题描述】:

我只想要创建对象的人,例如一个注册表来查看一些东西=,但努力用设计来定义一切

代码如下:

<% if user_signed_in? && ***"current_user = registry the user created?"?(struggling with the second part)*** %>

提前致谢

附:我正在使用 cancan,但这对我想要显示页面的显示页面没有帮助。

【问题讨论】:

    标签: ruby-on-rails if-statement devise cancan


    【解决方案1】:

    这不是设计的一部分,而是康康舞的一部分。

    你需要在你的cancan能力文件中定义能力:

    user ||= User.new
    can :read, Registry, :user_id => user.id
    

    这种方式 cancan 的授权方法将允许您执行以下操作:

    @registry = Registry.find(params[:id])
    authorize! :read, @registry
    

    明显授权!如果用户无权访问此资源,将引发错误。

    如果您只是想在您使用的注册表视图中检查这一点:

    <% if can? :read, @registry %>
      do your thing here
    <% end %>
    

    顺便说一句,所有这些都记录在 cancan 头版上:https://github.com/ryanb/cancan

    【讨论】:

      猜你喜欢
      • 2021-09-29
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2016-02-03
      • 2020-12-30
      • 2017-02-22
      • 2019-05-06
      • 1970-01-01
      相关资源
      最近更新 更多