How to create a new role in Solidus or Spree

Jul 19, 2018 by ohiodn8

In this tutorial we will be adding a new role to a solidus project. At the moment my project is built on the solidus gem, but then, the solidus gem is built on spree. So, this tutorial will work for a spree or solidus project. 

A solidus/spree user defines only one role with cancan (or cancancan) - admin. You can check this by running rails console in the root of your project folder (on the terminal/cmd), and inside the console run=> 


That should be fine to view the admin role with an id of 1, or it may prompt you to run Spree::Role.connection, and then  Spree::Role.all


Still in the rails console, run =>

Spree::Role.create(name: 'vendor')

This will create a new role - vendor, and you can close the console.

Head over to your application folder and create  app/models/spree/ability.rb =>

require 'cancan'
module Spree
  class Ability
    include CanCan::Ability

    class_attribute :abilities
    self.abilities = Set.new

    # Allows us to go beyond the standard cancan initialize method which makes it difficult for engine
    def self.register_ability(ability)

    def self.remove_ability(ability)

    def initialize(user)

      # override cancan default aliasing (we don't want to differentiate between read and index)
      alias_action :delete, to: :destroy
      alias_action :edit, to: :update
      alias_action :new, to: :create
      alias_action :new_action, to: :create
      alias_action :show, to: :read
      alias_action :index, :read, to: :display
      alias_action :create, :update, :destroy, to: :modify

      user ||= Spree.user_class.new

      if user.respond_to?(:has_spree_role?) && user.has_spree_role?('admin')
        can :manage, :all
      elsif user.respond_to?(:has_spree_role?) && user.has_spree_role?('vendor') 
        can :manage, User
        can :manage, Product  
        can :display, Country
        can :display, OptionType
        can :display, OptionValue
        can :create, Order
        can :read, Order do |order, token|
          order.user == user || order.token && token == order.token
        can :update, Order do |order, token|
          !order.completed? && (order.user == user || order.token && token == order.token)
        can :display, CreditCard, user_id: user.id
        can :display, Product
        can :display, ProductProperty
        can :display, Property
        can :create, Spree.user_class
        can [:read, :update, :destroy], Spree.user_class, id: user.id
        can :display, State
        can :display, Taxon
        can :display, Taxonomy
        can :display, Variant
        can :display, Zone

      # Include any abilities registered by extensions, etc.
      Ability.abilities.merge(abilities_to_register).each do |clazz|
        merge clazz.new(user)

      # Protect admin role
      cannot [:update, :destroy], Role, name: ['admin']


    # you can override this method to register your abilities
    # this method has to return array of classes
    def abilities_to_register

In here, you gave the admin role the ability to manage all your resources and gave the vendor role the ability to mange your product and user resources.

Start your server and head over to =>


Login with your admin password and create a new user account. Beneath the email link, you should see admin and vendor. Select vendor (input the new email and password for the vendor account) and create. 

Now logout of your admin account, and head back to => 


This time use the vendor account details that you just create and you'll be greeted with image below =>

Ignore the Authorization Failure error, and click on the Products and Users tab on the left side of the screen. To give your vendor role more functionality/ability, edit your the ability.rb file. 

(Hope I covered everything!)

About ohiodn8

Ruby on Rails developer, AWS Engineer, anything fun, music, a little bit of mobile game. . .

Comments (4)

Good Read

6 months ago by Alvaroknogs

Thank you

6 months ago by ZuvilsRog

Thanks a lot for this tutorial. How can i create a separated route for the vendor, and not accessing it from the admin link? Thanks

almost 3 years ago by Hugens Louis

Great Tutorial! It works!! Thanks for sharing!!!

almost 3 years ago by fhms info