我正在開發一個應用程序使用Devise。 因爲我需要一個用戶界面來管理用戶,所以我還生成了一個控制器和關聯的視圖來執行用戶模型上的所有CRUD操作。Mass-Assignment角色問題在控制器規範
然後我創建一個「角色」字段,我使用CanCan和作爲一個羣體分配角色。
現在,我試圖讓所有的規格正確,我有這個測試
describe "POST create" do
describe "with valid params" do
it "creates a new User" do
expect {
post :create, {:user => valid_attributes}
}.to change(User, :count).by(1)
end
# ...
end
end
在執行時提出:
UsersController POST create with valid params creates a new User
Failure/Error: post :create, {:user => valid_attributes}
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: name, surname, role
# ./spec/controllers/users_controller_spec.rb:62:in `block (5 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:61:in `block (4 levels) in <top (required)>'
而且在我的控制器#創建方法定義爲 類UsersController < ApplicationController中 load_and_authorize_resource
# GET /users
# GET /users.json
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
# GET /users/1
# GET /users/1.json
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: @user }
end
end
# GET /users/new
# GET /users/new.json
def new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @user }
end
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new params[:user], :as => current_user.role.to_sym
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Utente creato con successo.' }
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
# PUT /users/1
# PUT /users/1.json
def update
respond_to do |format|
if @user.update_attributes(params[:user], :as => current_user.role.to_sym)
format.html { redirect_to @user, notice: 'Profilo aggiornato con successo.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
end
爲了使rspec與Devise正常運行,我沿用了the official doc,並且我還創建了宏(與此處所述的相同),除非我沒有兩個不同的FactoryGirl,但是我在創建角色時定義了一個,例如:
FactoryGirl.create(:user, role: :admin) # or role: :user
這是用戶模型
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :surname, :role, :as => :admin
devise :database_authenticatable, :recoverable, :rememberable, :trackable,
:validatable
VALID_ROLES = [:admin, :student]
validates_inclusion_of :role, in: VALID_ROLES
def is_admin?
role == "admin"
end
def is_student?
role == "student"
end
end
我該如何解決呢?它花了我一整天,我不能讓這種:-(
嗨,對不起。我忘了把用戶模型放在OP上。我現在編輯它。我知道我必須聲明attr_accessible,那就是我遇到的問題......我正在嘗試使用批量分配角色。 – user1978591