私のコントローラーには次のものがあります:
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"}
superadmin
またはとしてブラウザで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"}