1
在我的控制,我有:load_resource得到一個零資源
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 => '[email protected]' }
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])
我看着黃燦燦的內部下整個流程和我發現在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
EDIT 2
在呼籲POST創建我有:
在RSpec的環境:
resource_base: User
resource_class: User
@params: {"user"=>{"email"=>"[email protected]"}, "controller"=>"users", "action"=>"create"}
在瀏覽器superadmin
或admin
,這是相同的:
resource_base: User
resource_class: User
@params: {"utf8"=>"✓", "authenticity_token"=>"95qQ4H/+CLU96jCIO6U/YtgIQ5zWxE7pg0BedVMPSGk=", "user"=>{"email"=>"[email protected]", "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"}
最後的寶石也可以粘貼'ability.rb'? – AnkitG
@AnkitG肯定!剛剛添加。謝謝! – enricostn
控制器的名稱是什麼? –