LoginSignup
43
43

More than 5 years have passed since last update.

RailsにCMS機能を付け加えられる、LocomotiveCMS!

Posted at

ちなみにLocomotiveCMSはrails cmsでググって最初に見つかったもの。
Railsでアプリを作ってあげて、後は使う人に任せるという運用をしたくて探していた。
WordPressでもいいけど、PHPよりRubyがいいなぁみたいな時は選択肢の一つになると思う。

LocomotiveCMSにはLocomotiveCMS Engineというものの他に、簡単にブログが作れるLocomotive Wagonというコマンドラインツールがついているけれど、こちらはRailsとして動くわけではないので注意。

参考サイトとしては、以下の2つがとても役立った。

ローカルの環境構築Mac、サーバーはHerokuを想定。

事前準備:MongoDBの設定

まずはインストール。

$ brew install mongodb

次に起動設定。

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/mongodb/2.2.3-x86_64/homebrew.mxcl.mongodb.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

きちんと立ち上がったか確認する。

$ ps x | grep mongodb
 14968   ??  S      0:00.41 /usr/local/opt/mongodb/mongod run --config /usr/local/etc/mongod.conf
 14983 s004  R+     0:00.00 grep --color=auto mongodb

新規Railsプロジェクト作成

  • 現状railsは3.2系列で無いと動かない。4.0系列で作られると困る
  • DBはactive_recordじゃなく、mongodbを使う
  • テストはrspecを使いたい。

ので、以下のように作製する。

$ rails _3.2.13_ new loco_site --skip-active-record --skip-test-unit --skip-bundle

もしくは、

$ rails _3.2.13_ new loco_site -O -T -B

LocomotiveCMSをローカルで動かす

Gemfileをちょっと書き換える。

  • rubyのバージョンを指定。(しないと後々HerokuでWarningが出る。)
  • locomotive_cms, unicorn, rspec-railsを追加。
  • locomotive_cmsに従ってassets groupにcompass-railsを追加。
    • 本当はpryとかbetter-errorsとかも追加するけどまぁいいや。

コメントを取った後のGemfileは以下のようになる。

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

gem 'rails', '3.2.13'

gem 'locomotive_cms', require: 'locomotive/engine'
gem 'thin'
gem 'rspec-rails'

group :assets do
  gem 'compass-rails',  '~> 1.0.2'
  gem 'sass-rails',     '~> 3.2.4'
  gem 'coffee-rails',   '~> 3.2.2'
  gem 'uglifier',       '~> 1.2.4'
end

gem 'jquery-rails'

書き換えたら、

$ bundle install

Rspecの設定と

$ rails g rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb

LocomotiveCMSの設定もgenerate。

$ rails g locomotive:install
      create  config/initializers/locomotive.rb
      create  config/initializers/carrierwave.rb
      create  config/initializers/dragonfly.rb
      create  config/mongoid.yml
       route  
  mount Locomotive::Engine => '/locomotive', as: 'locomotive' # you can change the value of the path, by default set to "/locomotive"

      remove  public/index.html
===============================================================================

The Locomotive Engine has been correctly installed in your Rails application.

  1. Edit the main config files:

    - config/initializers/locomotive.rb
    - config/initializers/carrierwave.rb
    - config/initializers/dragonfly.rb
    - config/mongoid.yml
    - config/devise.yml
    - config/routes.rb

  2. Launch the server

    > bundle exec unicorn_rails

  3. Open your browser

    > open localhost:8080

  4. Follow the installation wizard steps

  5. Enjoy !

===============================================================================

とりあえず何の設定もいじる必要ないだろと起動してみると、

$ bundle exec unicorn_rails
I, [2013-07-07T14:49:13.214872 #62661]  INFO -- : listening on addr=0.0.0.0:8080 fd=9
I, [2013-07-07T14:49:13.215010 #62661]  INFO -- : worker=0 spawning...
I, [2013-07-07T14:49:13.216376 #62661]  INFO -- : master process ready
I, [2013-07-07T14:49:13.217791 #62681]  INFO -- : worker=0 spawned pid=62681
I, [2013-07-07T14:49:13.218408 #62681]  INFO -- : Refreshing Gem list
There is a configuration error with the current mongoid.yml.

Problem:
  No sessions configuration provided.
Summary:
  Mongoid's configuration requires that you provide details about each session that can be connected to, and requires in the sessions config at least 1 default session to exist.
Resolution:
  Double check your mongoid.yml to make sure that you have a top-level sessions key with at least 1 default session configuration for it. You can regenerate a new mongoid.yml for assistance via `rails g mongoid:config`.

 Example:
   development:
     sessions:
       default:
         database: mongoid_dev
         hosts:
           - localhost:27017

と言ったエラーが出る。全くEnjoy出来ない。mongoid.ymlの設定の仕方が昔と変わったみたい。

とりあえずエラーが親切なので、

rails g mongoid:config

とやってconfig/mongoid.ymlを上書きしてやれば問題は解消。

open localhost:8080

とやってブラウザでlocalhost:8080を見ればウィザード画面が出ているはず!

おしまい!!

43
43
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
43
43