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 (20)

У Нас скидки круглый год!
HoOkAh MaGic наш официальный бренд
Набрав в поисковике Вы можете посетить наш сайт
и возможно приобрести кальяны и всё для них.
Персональные скидки
Строго 18+
На самом ли деле арома- кальян безопасен для здоровья?

6 days ago by Michaelreuri

comphifosi1984, Хочу опытного мужика, мне 18 лет.
Фото сомной
Хочу мужчину... 43ab8e2

Агата, И мне такого... мне скоро 18 лет.

2 months ago by Eleanorslova

Windows 10 update
[url=https://vaibhavethnic.com/10861/]Click here!..[/url]

2 months ago by Aprillped


3 months ago by FloydAmopy


3 months ago by FloydAmopy


3 months ago by FloydAmopy

Серебряные графины

3 months ago by FloydAmopy

[url=https://stolovoye-serebro.ru/product-category/stolovoe-serebro/chajnaja-posuda/podstakanniki/ ]Серебряные подстаканники[/url]

3 months ago by FloydAmopy

Лечение наркомании в Нальчике

3 months ago by FloydAmopy

Лечение наркозависимых во Владикавказе

3 months ago by FloydAmopy

Реабилитационный центр для зависимых в КБР

3 months ago by FloydAmopy

IonCube Decoder / Source Guardian / Zend / TrueBug

PHP 5.6 / 7.0 / 7.1 / 7.2 / 7.3 / 7.4 ALL PHP verions we can DECODE

Decompile prices:
1-20 files = 10$ / file
21-50 files = 9$ / file
51-100 files = 8$ / file
101 and more = 7$ / file

Forum verified: https://zerocode.su/index.php?threads/ioncube-decoding-and-deobfuscation-php.9/

telegram: MR_Fredo
Jabber : mr_fredo@thesecure.biz

3 months ago by Michaellache

Sell casino script Goldsvet 8.X NULLED. SOURCE CODE

A new version of GOLDSVET / mrs system. In this issue you will find new features for managing games and the system, the unification of
game banks, a significant increase in performance and a decrease in the load on the server and database. Several new unique interfaces,
as well as updating the Laravel Framework to version 8.14.0.

Our casino script:
1) has no domain restrictions.
2) all games are open source.
3) 960+ games in total.
4) all games work on a computer and on a phone, and have html5 format.

Casino price: 2500 EUR
Goldsvet MRS Core 8.0 DECODED (NULLED) + GAMES
Video review: https://youtu.be/-ZcD8Jf3YN4
telegram: https://t.me/kseGB
mail: saportcasino@protonmail.com
site: http://win-game.net/

3 months ago by Jasonmeada

Great Content. Works well

6 months ago by CharlesOwept

Good Read

12 months ago by Alvaroknogs

Thank you

12 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

about 3 years ago by Hugens Louis

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

over 3 years ago by fhms info