Cancan无法授权创建操作

时间:2013-09-17 04:07:07

标签: ruby-on-rails-3.2 cancan

这是我的控制器

class IdeasController < ApplicationController
  load_and_authorize_resource

  def create
    @idea = Idea.create params[:idea]
  end
end

我的能力档案是

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.confirmed?  
      can :manage, Idea
    end
  end
end

但是我的规范在创建

时返回成功
context "when not logged in"do
  it "does not be allowed" do
    post :create, FactoryGirl.attributes_for(:idea)
    response.should_not be_success
  end   
end

我需要在控制器中添加before_filter :authenticate_user!吗?

1 个答案:

答案 0 :(得分:0)

在能力模型中尝试:cannot :create, Idea

更新:

设置具有用户身份验证的控制器需要

before_filter :authenticate_user!