1

在我的控制器中,我有:

class UsersController < ApplicationController

  load_and_authorize_resource

  def create

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

但我明白了undefined method 'save' for nil:NilClass

Failures:

1) UsersController if the user passes all the authorizations POST #create should create a new User with some parameters
 Failure/Error: post :create, :user => { :email => 'puppa@puppa.pup' }
 NoMethodError:
   undefined method `save' for nil:NilClass
 # ./app/controllers/users_controller.rb:47:in `block in create'
 # ./app/controllers/users_controller.rb:46:in `create'
 # ./spec/controllers/users_controller_spec.rb:66:in `block (4 levels) in <top (required)>'

Finished in 0.10714 seconds
1 example, 1 failure

我期待load_resources填充@user = User.new(params[:user])

我在整个流程之后查看 CanCan 内部结构,我发现controller_resource.rb当我们到达时,#build_resource我们有:

def build_resource
  resource = resource_base.new(resource_params || {})
  assign_attributes(resource)
end

但是resource这里nil……这很正常吗?我错过了什么?这与我的创建操作问题有关吗?

编辑

这是我的ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:

    # check if the user is registered or a guest user (not logged in)
    if user.present?

      if user.any_role? :super_admin
        can :manage, :all
      end

      if user.any_role? :admin
        can :manage, [User, Institution, Project, Order]
      end

      if user.any_role? :user
        can :show, Project
        can [:add, :change], :cart
        can [:create, :show], Order, :user_id => user.id
        can :download, UrlConnector
      end

    end
  end
end

编辑 2

在调用 POST create 时,我有:

在 RSpec 环境中:

resource_base: User
resource_class: User
@params: {"user"=>{"email"=>"puppa@puppa.pup"}, "controller"=>"users", "action"=>"create"}

在浏览器中作为superadminor admin,它是相同的:

resource_base: User
resource_class: User
@params: {"utf8"=>"✓", "authenticity_token"=>"95qQ4H/+CLU96jCIO6U/YtgIQ5zWxE7pg0BedVMPSGk=", "user"=>{"email"=>"estanost@alumnes.ub.edu", "password"=>"264763", "password_confirmation"=>"264763", "ragionesociale"=>"fff", "partitaiva"=>"12345678901", "address"=>"via plutarco, 36", "city"=>"Manduria", "cap"=>"74024", "phone"=>"099979456", "role_ids"=>["3"]}, "commit"=>"Create User", "action"=>"create", "controller"=>"users"}
4

2 回答 2

1

尝试将 cancan 作为 Gemfile 中的最后一个 gem

于 2012-11-06T02:55:13.350 回答
0

我认为您应该摆脱该if user.present?块,添加user ||= User.newif块开始的位置。这样,您将始终拥有一个用户对象。当然,它不会通过你的任何康康舞,所以其余的应该优雅地通过。

于 2021-03-20T15:31:22.320 回答