I'm trying to figure out how to use pundit in my Rails 4 app.
I have a profile view in which I want to display a link to create a new project, subject to pundit authorisation.
I have tried each of the following formulations:
<%# if policy(Project.new).create? %>
<%# if policy(Project).create? %>
<%# if policy(@project).create? %>
<%# if policy(Projects).create? %>
<% if policy(project).create? %>
<%= link_to 'CREATE A PROJECT', new_project_path, :class=>"btn btn-info" %>
<% end %>
The association between project and profile is:
Project
belongs_to :profile
Profile
has_many :projects, dependent: :destroy
My project policy has:
def new?
true
# create?
end
def create?
true
end
when I try using this line in the profile view : <% if policy(Project).create? %>
I get an error that says:
wrong number of arguments (given 2, expected 0)
when I try using this line in the profile view : <% if policy(@project).create? %>
I get an error that says:
wrong number of arguments (given 2, expected 0)
when I try using this line in the profile view : <% if policy(Projects).create? %>
I get an error that says:
unable to find policy of nil
when I try using this line in the profile view : <% if policy(project).create? %>
I get an error that says:
undefined local variable or method `project' for <#:0x007faf5255d468> Did you mean? project_url
Is there something special I need to do to test the project authorisation if the view page is in a different model (e.g. profile, testing authorisation for that profile as to whether it can create a project)? I'm stuck and guessing as to how to solve this problem.
In my projects controller, I have a create method with:
def create
@project = Project.new(project_params)
@project.profile = current_user.profile
respond_to do |format|
if @project.save
format.html { redirect_to @project }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
The initialiser for the policy:
class ProjectPolicy < ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end