gpt4 book ai didi

ruby-on-rails - Rails cancan gem 不会只授权新 Action

转载 作者:行者123 更新时间:2023-12-02 03:34:19 24 4
gpt4 key购买 nike

使用 cancan 我无法创建新记录。我厌倦了阅读文档,但找不到任何帮助。

class Ability
include CanCan::Ability

def initialize(user)
if user.nil?
can :read, Branch
can :read, Leaf
elsif user.role? "admin"
can :manage, :all
else
can :manage, Branch, :user_id => user.id
can :manage, Leaf, :branch => { :user_id => user.id }

# Also can read all.
can :read, :all
end
end

Controller :

before_filter :authenticate_user!, :except => [:index, :show]

  def new
@branch = Branch.new
authorize! :new, @branch, :message => 'You are not authorized to perform this action.'
respond_to do |format|
format.html # new.html.erb
format.json { render json: @branch }
end
end

def create
@branch = Branch.new(branch_params)
authorize! :create, @branch, :message => 'You are not authorized to perform this action.'

respond_to do |format|
if @branch.save
format.html { redirect_to user_branches_path(current_user.username), notice: 'Branch was successfully created.' }
else
format.html { render action: 'new'}
end
end
end

最佳答案

我猜您使用的是 Rails 4,默认情况下它使用强参数,它不能很好地与 CanCan 配合使用。其概述在此 blog .尝试 CanCanCan这是已死的 CanCan 项目的延续。

关于ruby-on-rails - Rails cancan gem 不会只授权新 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24665112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com