LoginSignup
6
2

More than 3 years have passed since last update.

【実践】SolidusでRails製ECサイトを作ってみる

Last updated at Posted at 2020-01-13

SolidusはSpreeの後継のRails製オープンソースECサイトのプロジェクト。
OSSを使わないとしても、ECに最低限の機能が入ってるので、自分たちで機能の洗い出しするより、これを参考にした方が早い。

準備

以下をインストールしていない人はインストール

Rubyはバージョンが低いとfiniteのエラーが出ます。
テスト環境ではv2.5.1を使用しました。

brew install sqlite3 imagemagick

1. Railsアプリを作成

rails new myshop --skip_webpack_install
cd myshop

2. Gemfileに以下を追加

Gemfile
gem 'solidus'
gem 'solidus_auth_devise'

'solidus'をrequireすると、以下がまとめてインストールされます。個別にインストールすることも可能なよう。
solidus_core
solidus_api
solidus_frontend
solidus_backend
solidus_sample

bundle install

3. gemのinitialize & マイグレーション

bundle exec rails generate spree:install
# ここでadmin / パスワードを設定します

bundle exec rails generate solidus:auth:install
bundle exec rake railties:install:migrations

bundle exec rake db:migrate

spreeはsolidusの元のOSS eCommerceプロジェクトです。
マイグレーションも作成されます。

4.Viewをオーバーライドする

bundle exec rails generate solidus:views:override

# bundle exec rails generate solidus:views:override --only products/show
# 一部だけカスタマイズする場合はこのようなコマンド

起動してみる

bundle exec rails server

管理画面について

adminユーザーでログインし、/adminにアクセスすると、管理画面が表示されます。

スクリーンショット 2020-01-13 16.41.11.png

APIについて

APIリファレンスはこちら
https://solidus.docs.stoplight.io/

商品のレスポンス例:


{
    "count": 18,
    "total_count": 18,
    "current_page": 1,
    "pages": 1,
    "per_page": 25,
    "products": [
        {
            "id": 1,
            "name": "Ruby on Rails Tote",
            "description": "Soluta sed error debitis repellendus et. Voluptates unde enim qui velit. Libero earum tenetur nulla similique temporibus quod repellendus quibusdam.",
            "available_on": "2020-01-13T07:32:59.433Z",
            "slug": "ruby-on-rails-tote",
            "meta_description": null,
            "meta_keywords": null,
            "shipping_category_id": 1,
            "taxon_ids": [
                3,
                10,
                13,
                20
            ],
            "meta_title": null,
            "total_on_hand": 10,
            "price": "15.99",
            "display_price": "$15.99",
            "has_variants": false,
            "master": {
                "id": 1,
                "name": "Ruby on Rails Tote",
                "sku": "ROR-00011",
                "weight": "0.0",
                "height": null,
                "width": null,
                "depth": null,
                "is_master": true,
                "slug": "ruby-on-rails-tote",
                "description": "Soluta sed error debitis repellendus et. Voluptates unde enim qui velit. Libero earum tenetur nulla similique temporibus quod repellendus quibusdam.",
                "track_inventory": true,
                "cost_price": "17.0",
                "price": "15.99",
                "display_price": "$15.99",
                "options_text": "",
                "in_stock": true,
                "is_backorderable": true,
                "total_on_hand": 10,
                "is_destroyed": false,
                "option_values": [],
                "images": []
            },
            "variants": [],
            "option_types": [],
            "product_properties": [
                {
                    "id": 25,
                    "product_id": 1,
                    "property_id": 9,
                    "value": "Tote",
                    "property_name": "Type"
                },
                {
                    "id": 26,
                    "product_id": 1,
                    "property_id": 10,
                    "value": "15\" x 18\" x 6\"",
                    "property_name": "Size"
                },
                {
                    "id": 27,
                    "product_id": 1,
                    "property_id": 11,
                    "value": "Canvas",
                    "property_name": "Material"
                }
            ],
            "classifications": [
                {
                    "taxon_id": 3,
                    "position": 1,
                    "taxon": {
                        "id": 3,
                        "name": "Bags",
                        "pretty_name": "Categories -> Bags",
                        "permalink": "categories/bags",
                        "parent_id": 1,
                        "taxonomy_id": 1,
                        "taxons": []
                    }
                },
                {
                    "taxon_id": 10,
                    "position": 1,
                    "taxon": {
                        "id": 10,
                        "name": "Rails",
                        "pretty_name": "Brand -> Rails",
                        "permalink": "brand/rails",
                        "parent_id": 2,
                        "taxonomy_id": 2,
                        "taxons": []
                    }
                },
                {
                    "taxon_id": 13,
                    "position": 1,
                    "taxon": {
                        "id": 13,
                        "name": "Bags",
                        "pretty_name": "Categories -> Bags",
                        "permalink": "categories/bags",
                        "parent_id": 1,
                        "taxonomy_id": 1,
                        "taxons": []
                    }
                },
                {
                    "taxon_id": 20,
                    "position": 1,
                    "taxon": {
                        "id": 20,
                        "name": "Rails",
                        "pretty_name": "Brand -> Rails",
                        "permalink": "brand/rails",
                        "parent_id": 2,
                        "taxonomy_id": 2,
                        "taxons": []
                    }
                }
            ]
        },
...

まとめ

詳しくはもう少しいじって追記していく予定。

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2