CanCan load_and_authorize_resource uruchamia zabronione atrybuty

Mam standardowy kontroler REST, który używa silnych parametrów.

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

W moimconfig/initializers Mam plikstrong_parameters.rb

ActiveRecord::Base.send(:include,  ActiveModel::ForbiddenAttributesProtection)

Kiedy dodam proste połączenie do CanCanaload_and_authorize_resource dostaję

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)>'

Gdzie@attr w teście jest zdefiniowany jako

  before(:each) do
    @attr =
      {
        first_name: "John",
        last_name: "Doe",
        email: "[email protected]",
        password: "foobar",
        password_confirmation: "foobar"
      }
  end

W testach mam wszystko poprawnie skonfigurowane, aby zalogować się do użytkownika i dać im niezbędne role do bycia administratorem, więc wiem, że tak nie jest. Nie wiem, dlaczego powoduje to wyzwalanie ForbiddenAttributes. Jestem pewna, że ​​przeoczyłem coś prostego. Czy ktoś inny napotkał ten problem i znalazł rozwiązanie?

questionAnswers(2)

yourAnswerToTheQuestion