Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:15:49+00:00 2026-06-17T10:15:49+00:00

I’m developing an application using Devise. Since I need a UI to manage users

  • 0

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.

Then I create a “role” field that I use with CanCan and as a mass-assignment role.

Now I’m trying to make all specs properly, I have this test

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

That when executed raises:

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

And where my controller’s #create method is defined as
class 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

To make rspec run properly with Devise I followed the official doc 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:

FactoryGirl.create(:user, role: :admin) # or role: :user

And this is the User model

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

How can I fix that? It took all my day and I couldn’t get this working 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:15:51+00:00Added an answer on June 17, 2026 at 10:15 am

    The issue was caused by cancan’s load_and_authorize_resource that is calling User.create without allowing me to declare the “:as” clause containing the role.

    The fix was actually to remove the call to load_and_authorize_resource and create the resource in a typical way (as you would do without cancan). Then call in each method the authorization check, that is authorize! :create, User for #create.

    Probably it would also work by using something like load_and_authorize_resource :except => [:create] and then do it manually only there, but I’m not sure and I didn’t test it.

    By the way I’ve lost a working day on this bug and I really hope this can help someone else having the same issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.