On this Ruby on Rails tutorial, we will talking about Solidus. If you are new with Ruby on Rails, you might want to read this: What is Ruby On Rails as the Leading Programming Language Framework
So..different Rails have different opinion when people discuss about using Ruby On Rails for backed for the e-commerce project. Some think wordpress is better than that. But think about the brands like Shopify, they are the giant companies that are using Rails as the platform and serving millions of customers without any problem. In this article we are going to discuss about using Ruby On Rails as the e-commerce platform. We are going to take the advantage of the solidus gem which is the fork of spree ecommerce. This gem ships with the tools we need to set up a production ready mountable ecommerce engine.
Make sure you have Imagemagic installed in your system before installing Solidus. After that create a new rails project and put these gems into your Gemfile.
Run the bundler.
|$ bundle install|
You have installed the gems but now you need to run the generators to create the necessary migrations and configurations. Run the following commands one by one
|$ bundle exec rails g spree:install
$ bundle exec rails g solidus:auth:install
$ bundle exec rake railties:install:migrations
When you run the first command to install solidus, it will prompt you to enter email and password. That is to protect the admin section of your app.
Now run the migrations, solidus must have created bunch of migration files for you.
|$ rails db:migrate|
Note that if you want to use postgresql for the development database locally too, you can do that the configurations you need to make is to specify the db username and password in database.yml file.
I think everything must be up and running now. Fire up your terminal and run the rails server. Then navigate to localhost:3000 .The admin section of your app exists at localhost:3000/admin .
Solidus run slow in development environment but it works fine in production so there is nothing to worry about.
Admin interface contains lot of things for managing your store. You can view the orders you get, set the payment options, see the users that are signed up for the app, manage your stocks and products, promotion options and more. You can also add more features on your own form it.
We want to tweak the solidus gem little bit. First let’s change the default logo for our store. Copy your logo to the logo directory in app/assets/images/logo/pic.png .Next, you need to change the name of logo image in the config/initializers/spree.rb
|Spree.config do |config|
# Custom logo for the frontend
# config.logo = “logo/solidus_logo.png”
config.logo = “logo/pic.png”
Note that you will need to restart the server whenever you change something in initializers file.
Now let’s customize the front end of our application. We want to install bootstrap to our store. So in the Gemfile add the following line
|gem ‘bootstrap-sass’, ‘~> 3.3.6’|
Then run the bundler to fetch the gem. One thing is tricky here, Solidus has a different manifest files to add the import statements. In regular rails app we used to add import statements in assets/application.css.scss. But for this add the statements in assets/stylesheets/spree/fontend/all.css and assets/javascrpits/spree/frontend/all.js . If you don’t already have these files and folders, create it.
//= require jquery
//= require jquery_ujs
//= require spree
# In the assets/stylesheets/spree/frontend/all.scss
In order to customize a view you should copy the file into your host app. Using Deface is not recommended as it provides lots of headaches while debugging and degrades your shops performance. Solidus provides a generator to help with copying the right view into your host app.
Simply call the generator to copy all views into your host app.
|$ rails g solidus:views:override|
If you only want to copy certain views into your host app, you can provide the –only argument:
|$ bundle exec rails g solidus:views:override –only products/show|
The argument to –only can also be a substring of the name of the view from the app/views/spree folder:
|$ bundle exec rails g solidus:views:override –only product|
After generating the views you can choose how you want to display the products and you can add custom behaviour to your store.
In case you want to translate a string depending on the locale.Solidus provides the internationalization gem for this issue.
|gem ‘solidus_i18n’, github: ‘solidusio-contrib/solidus_i18n’, branch: ‘master’|
Run the bundler. Then run the installer for the gem.
|$ bin/rails g solidus_i18n:install|
You can also set the default locale within config/initializers/spree.rb .
|Spree::Frontend::Config.configure do |config|
config.locale = ‘en-GB’
Spree::Backend::Config.configure do |config|
config.locale = ‘en-GB’
You can read more about using the gem here.
Solidus provides you with a bunch of add-ons or extensions that can be used to extend your store. Let’s see an example of using the extension. We want to integrate the payment gateway extension.
Enter the following line in the Gemfile.
Then run bundler and installer.
rails g solidus_gateway:install
Finally, make sure to restart your app. Navigate to Settings > Payments > Payment Methods in the admin panel. You should see a number of payment methods and the assigned provider for each. Click on the payment method you wish to change the provider, and you should see a number of options under the provider dropdown.
You can find more extensions here.