Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 如何通过Pundit授权Rails中自定义创建的方法?_Ruby On Rails 4_Pundit - Fatal编程技术网

Ruby on rails 4 如何通过Pundit授权Rails中自定义创建的方法?

Ruby on rails 4 如何通过Pundit授权Rails中自定义创建的方法?,ruby-on-rails-4,pundit,Ruby On Rails 4,Pundit,我已经在rails 4中创建了自定义方法 def duplicate new_house = @house.amoeba_dup respond_to do |format| if new_house.save format.html { render action: 'new', notice: 'Category Attribute Added Successfully' } else

我已经在rails 4中创建了自定义方法

def duplicate    
    new_house = @house.amoeba_dup  
    respond_to do |format|
        if new_house.save        
           format.html { render action: 'new', notice: 'Category Attribute Added Successfully' }
        else
           format.html { render action: 'new' }        
        end
    end    
end
但当我调用复制方法时,它会给出
Pundit::AuthorizationNotPerformedError

发生这种情况是因为检测到您的新控制器方法没有检查授权。这通常由控制器中的以下行触发:

操作后:验证是否已授权

因此,请将新方法更改为:

def duplicate    
  new_house = @house.amoeba_dup
  authorize new_house
  respond_to do |format|
    if new_house.save        
      format.html { render action: 'new', notice: 'Category Attribute Added Successfully' }
    else
      format.html { render action: 'new' }        
    end
  end    
end
您还需要更新您的
house\u policy.rb
以添加
replicate?
方法。下面的示例假定权限与创建方法的权限相同:

# policies/house_policy.rb
class HousePolicy < ApplicationPolicy
  def duplicate?
    create?
  end
#政策/house_policy.rb
类HousePolicy<应用程序策略
def副本?
创造?
结束