ruby-on-railsspreealchemy-cmsalchemy-spree

No routes work after adding Alchemy CMS to Spree project


I added Alchemy CMS to my Spree project that was previously functioning ok. But now none of the routes work. For all routes I get a page not found error. I've done the install and ran all of the migrations.

error

Alchemy::Page not found "/"

Gemfile

ruby '2.2.4'
source 'https://rubygems.org'

gem 'rails', '4.2.5'
gem 'pg', '~> 0.15'
gem 'sass-rails'
gem 'uglifier'
gem 'coffee-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'active_model_serializers'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spree', github: 'spree/spree', branch: 'master'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise'
gem 'puma'
gem 'paperclip'
gem 'aws-sdk', '< 2.0'
gem 'delayed_job_active_record'

gem 'alchemy_spree', github: 'magiclabs/alchemy_spree', branch: 'master'
# gem 'alchemy_cms', github: 'AlchemyCMS/alchemy_cms', branch: '3.1-stable'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

config/routes.rb

Rails.application.routes.draw do
  mount Alchemy::Engine => '/'
  mount Spree::Core::Engine, :at => '/shop'
end

config/initializer/alchemy.rb

# Tell Alchemy to use the Spree::User class
Alchemy.user_class_name = 'Spree::User'
Alchemy.current_user_method = :spree_current_user

# Load the Spree.user_class decorator for Alchemy roles
require 'alchemy/spree/spree_user_decorator'

# Include the Spree controller helpers to render the
# alchemy pages within the default Spree layout
Alchemy::BaseHelper.send :include, Spree::BaseHelper
Alchemy::BaseController.send :include, Spree::Core::ControllerHelpers::Common
Alchemy::BaseController.send :include, Spree::Core::ControllerHelpers::Store

config/application.rb

module DistinctExistence
  class Application < Rails::Application
    config.to_prepare do
      # Load application's model / class decorators
      Dir.glob(File.join(File.dirname(__FILE__), '../app/**/*_decorator*.rb')) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end

      # Load application's view overrides
      Dir.glob(File.join(File.dirname(__FILE__), '../app/overrides/*.rb')) do |c|
        Rails.configuration.cache_classes ? require(c) : load(c)
      end

      Spree::UserSessionsController.class_eval do
        include Alchemy::ControllerActions
      end
    end
    config.time_zone = 'Central Time (US & Canada)'
    config.active_job.queue_adapter = :delayed_job
    config.active_record.raise_in_transactional_callbacks = true
  end
end

config/initializers/spree.rb

Spree.config do |config|
  # Example:
  # Uncomment to stop tracking inventory levels in the application
  # config.track_inventory_levels = false
end

Spree.user_class = 'Spree::User'

attachment_config = {

  s3_credentials: {
    access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
    bucket:            ENV['S3_BUCKET_NAME']
  },

  storage:        :s3,
  s3_headers:     { 'Cache-Control' => 'max-age=31557600' },
  s3_protocol:    'https',
  bucket:         ENV['S3_BUCKET_NAME'],
  url:            ':s3_domain_url',

  styles: {
    mini:     '48x48>',
    small:    '100x100>',
    product:  '240x240>',
    large:    '600x600>'
  },

  path:           '/:class/:id/:style/:basename.:extension',
  default_url:    '/:class/:id/:style/:basename.:extension',
  default_style:  'product'
}

attachment_config.each do |key, value|
  Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end

config/alchemy/element.yml

- name: article
  hint: true
  unique: true
  contents:
  - name: headline
    type: EssenceText
    default: :article_headline
    hint: true
  - name: picture
    type: EssencePicture
    hint: true
  - name: text
    type: EssenceRichtext
    default: :article_text
    hint: true

- name: product
  contents:
  - name: spree_product
    type: EssenceSpreeProduct

- name: product_category
  contents:
  - name: spree_taxon
    type: EssenceSpreeTaxon

config/alchemy.page_layouts.yml

- name: index
  unique: true
  elements: [article]
  autogenerate: [article]

- name: product
  elements: [product]
- name: products
  elements: [product_category]

Solution

  • Alchemy has a very strong catch all route. Please mount Alchemy as last Engine in your routes file:

    Rails.application.routes.draw do
      mount Spree::Core::Engine, :at => '/shop'
      mount Alchemy::Engine => '/'
    end
    

    Then all routes of each specific engine will be accessible under it's route prefix.

    So, to access Spree admin you need to use /shop/admin, while Alchemy routes are all accessible directly under /.

    NOTE: To make redirecting to login paths work, you need to tell Alchemy about this. Following the example above and if you are using spree_auth_devise:

    # config/initializers/alchemy.rb
    Alchemy.login_path = '/shop/login'
    

    or, if you use your own custom authentication:

    # config/initializers/alchemy.rb
    Alchemy.login_path = '/sessions/new'