使用康康舞,我无法创建新记录。我已经厌倦了从文档中阅读,但是我找不到任何帮助。

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

最佳答案

我想你在默认情况下使用强参数的4轨道上,它与CanCan不能很好地配合使用。它在此blog中概述。尝试CanCanCan,它是死掉的CanCan项目的延续。

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

10-11 04:45