The who, what, where

Projects & Talks

Adding Spree With Custom User

Adding Spree E-commerce to an existing Rails application that uses Devise and a custom User model

These steps document this process for my own records, but anyone is welcome to use them. I’ve taken these steps from a few places:

1) Spree Docs Getting Started 2) Spree Docs Custom Auth 3) Devise Docs

cat "gem 'spree', '~> 3.3.0'" > Gemfile
bundle install
bin/rails generate spree:install --user-class=User --no-sample --no-migrate --no-seed
bin/rails g spree:custom_user User
bin/rails db:migrate

At this point it’s time to customize our routes so Spree knows how to login and out. Open config/routes.rb and add:

devise_scope :person do
  get '/login', to: "devise/sessions#new"
  get '/signup', to: "devise/registrations#new"
  delete '/logout', to: "devise/sessions#destroy"

As well, for your custom User class to work you need to include some of the expected methods and relations by including some modules from Spree in your class definition. If you don’t do this you’ll find your missing many of the methods required for Spree to work properly (spree_roles, has_spree_role?, orders, last_incomplete_spree_order, etc.). For example,

class User < ApplicationRecord
  include Spree::UserAddress
  include Spree::UserMethods
  include Spree::UserPaymentSource
  include Spree::UserReporting

Finally I went back to finish the gateway install:

cat "gem 'spree_gateway', '~> 3.3'" > Gemfile
bundle install
bundle exec rails g spree_gateway:install

About Me

An image of me peeking up from the bottom of the frame

I'm a programmer and photographer.

I own Burmis Studio, and am one of the organizers of YEGRB, the Edmonton Ruby Meetup.

Find Me

Looking to get in touch? Try one of the communities below.