Adding a custom page to Spree Commerce Store

Ruby Gems are great; I love ’em but not so much when it comes to maintenance. I also love Spree. It gives me the flexibility to do what I what — how I want. I wanted to add custom pages to my upcoming online store, iMayne Robotics, without the use of a gem and confusing controller.

To do this, my pages should be using all the head contents of Spree. Mine will look a bit different as I’ve not included everything important as of yet. Make sure you have Spree installed; what that hell, let’s quickly whip that up (Stewie’s pronunciation)

rails new my_app # my_app is your new folder then cd my_app

Let’s install spree:

gem 'spree', '~> 3.1.0'
gem 'spree_auth_devise', '~> 3.1.0'
gem 'spree_gateway', '~> 3.1.0'
# Now bundle install (from terminal) then...
rails g spree:install --user_class=Spree::User
rails g spree:auth:install
rails g spree_gateway:install

If you get any errors, let me hold your hand:

In terminal, after you have added the gem to your Gemfile, save that file then open up your terminal, make sure you are in the same folder, then type:

bundle install

After, you would then type (line by line):

rails g spree:install --user_class=Spree::User
rails g spree:auth:install
rails g spree_gateway:install

Just press enter to accept defaults. The password will be spree123.

The moment you’ve been waiting for! Custom pages!

First I made a custom application.html.rb file and share that with spree’s layouts and my pages’s layouts. That way both spree and my pages uses the same information that’s in spree’s head.

Now my application would render that custom partial:

You will now need to pull in spree’s layouts from the gem by adding the folders “spree/layouts/”:

That’s it! Now add your controllers and pages like you’d normally do. In your routes.rb file, place your routes after where the spree store is mounted.

When linking to your custom page ie faq_path, you must place “main_app” in front:

<%= link_to "Faq Page", main_app.faq_path %>

There you have it, your customised spree pages. Just do your normal rails stuff. ;)