我想弄清楚如何在我的Rails 4应用程序中使用pundit。Rails 4 - 权威 - 创建策略
我有一个配置文件视图,其中我想显示一个链接来创建一个新项目,并受权限授权。
我曾尝试以下每个配方:
<%# 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 %>
项目和配置文件之间的关联是:
项目
belongs_to :profile
档案
has_many :projects, dependent: :destroy
我的项目的政策有:
def new?
true
# create?
end
def create?
true
end
当我尝试使用该线路纵断面图:<% if policy(Project).create? %>
我得到一个错误,指出:
错误的参数数目(假设2,预计0)
当我尝试在配置文件视图中使用此行:<% if policy(@project).create? %>
我得到一个错误,指出:
错误的参数数目(假设2,预计0)
当我尝试使用在纵断面图中这条线:<% if policy(Projects).create? %>
我得到一个错误,说:
无法找到零的政策
当我尝试在纵断面图中使用此行:<% if policy(project).create? %>
我得到一个错误,指出:
未定义的局部变量或方法'项目”的 <#:0x007faf5255d468>难道你意思是? project_url
是不是有什么特别的,我需要做测试的项目授权,如果视图页面在不同的模式(如曲线,测试授权该配置文件至于是否可以创建一个项目)?我卡住了,并猜测如何解决这个问题。
在我的项目控制,我有一个创建方法:
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
该策略的初始化器:
class ProjectPolicy < ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
你的'def create'方法是什么? –
@KieranAndrews - 我在文章中添加了创建动作 – Mel
您的'link_to'CREATE A PROJECT''文件是什么视图文件? –