我有一个使用强参数的标准 RESTful 控制器。
class UsersController < ApplicationController
respond_to :html, :js
def index
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def edit
@user = User.find(params[:id])
end
def create
@user = User.new(safe_params)
if @user.save
redirect_to @user, notice: t('users.controller.create.success')
else
render :new
end
end
def update
@user = User.find(params[:id])
if @user.update_attributes(safe_params)
redirect_to @user, notice: t('users.controller.update.success')
else
render :edit
end
end
def destroy
@user = User.find(params[:id])
if current_user != @user
@user.destroy
else
flash[:error] = t('users.controller.destroy.prevent_self_destroy')
end
redirect_to users_url
end
private
def safe_params
safe_attributes =
[
:first_name,
:last_name,
:email,
:password,
:password_confirmation,
]
if current_user.is?(:admin)
safe_attributes += [:role_ids]
end
params.require(:user).permit(*safe_attributes)
end
end
在我的config/initializers
我有文件strong_parameters.rb
ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)
当我向 CanCan's 添加一个简单的调用时,load_and_authorize_resource
我得到
1) UsersController POST create with invalid params re-renders the 'new' template
Failure/Error: post :create, user: @attr
ActiveModel::ForbiddenAttributes:
ActiveModel::ForbiddenAttributes
# ./spec/controllers/users_controller_spec.rb:128:in `block (4 levels) in <top (required)>'
在@attr
测试中定义为
before(:each) do
@attr =
{
first_name: "John",
last_name: "Doe",
email: "user@example.com",
password: "foobar",
password_confirmation: "foobar"
}
end
在测试中,我已正确设置所有内容以登录用户并为他们提供成为管理员所需的角色,所以我知道不是这样。我不知道为什么这会导致 ForbiddenAttributes 触发。我敢肯定这是我忽略的一些简单的事情。有没有其他人遇到过这个问题并找到了解决方案?