Rails 4 - Pundit - create policy

▼魔方 西西 提交于 2019-12-02 08:46:54

It looks like your code may not have the necessary instance variables. Inside your show method you should have a @project - you can use Pundit to check wether the user can create it.

As you dont appear to have a @project, you can try this instead:

<% if policy(Project.new).create? %>

You could also try using a symbol instead:

policy(:project)

<% if policy(:dashboard).show? %>
  <%= link_to 'Dashboard', dashboard_path %>
<% end %>

Do you have a policy defined like so?

# app/policies/project_policy.rb
class ProjectPolicy < Struct.new(:user, :project)
  # ...
end

Usually I will use another class, say ViewPolicy, for the purpose in view:

class ViewPolicy < Struct.new(:user, :views)

    def items_index?
        user.has_role?(:sales)
    end

end

So I can do something like this:

<% if policy(:views).items_index? %>
  <%= link_to("Items", items_path) %>
<% end %>

very similar to @Kieran Andrews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!