Note that there are some explanatory texts on larger screens.

plurals
  1. POMass-Assignment Roles issue in controller spec
    primarykey
    data
    text
    <p>I'm developing an application using Devise. Since I need a UI to manage users I also generated a controller and associated views to perform all CRUD operations on the User model.</p> <p>Then I create a "role" field that I use with CanCan and as a mass-assignment role.</p> <p>Now I'm trying to make all specs properly, I have this test</p> <pre><code>describe "POST create" do describe "with valid params" do it "creates a new User" do expect { post :create, {:user =&gt; valid_attributes} }.to change(User, :count).by(1) end # ... end end </code></pre> <p>That when executed raises: </p> <pre><code>UsersController POST create with valid params creates a new User Failure/Error: post :create, {:user =&gt; 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 &lt;top (required)&gt;' # ./spec/controllers/users_controller_spec.rb:61:in `block (4 levels) in &lt;top (required)&gt;' </code></pre> <p>And where my controller's #create method is defined as class UsersController &lt; ApplicationController load_and_authorize_resource</p> <pre><code> # 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 =&gt; 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 =&gt; 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 </code></pre> <p>To make rspec run properly with Devise I followed <a href="https://github.com/plataformatec/devise/wiki/How-To%3a-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29" rel="nofollow">the official doc</a> and I also created macros (the same as described there), except I don't have two different FactoryGirl but one where I define on creation the role, for example:</p> <pre><code>FactoryGirl.create(:user, role: :admin) # or role: :user </code></pre> <p>And this is the User model</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :email, :password, :password_confirmation, :remember_me attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :surname, :role, :as =&gt; :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 </code></pre> <p>How can I fix that? It took all my day and I couldn't get this working :-(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload