クラウドIDE(統合開発環境)cloud9でRuby on Railsを動かす方法をメモしておきます。
参考文献:Ruby on Rails Tutorial 日本語訳
確認環境:
cloud9
Rails version: 5.1.3
Ruby version: 2.4.0 (x86_64-linux)
Railsをインストールする
Railsのインストールにはgemコマンドを使います。
(このコマンドは、RubyGemsパッケージマネージャが提供してます。)
インストールには以下のコマンドを実行します。
$ gem install rails -v 5.1.2
-vオプションでバージョンを指定できます。
「workspace」フォルダを作る
$ cd
$ mkdir workspace
$ cd workspace
rails newを実行する
$ rails _5.1.2_ new hello_app
→ 大量のファイルとディレクトリが作成される。
アプリケーションに必要なgemをインストールする
\hello_app\Gemfileを以下の通りに編集する。
gem 'rails', '5.1.2'
gem 'sqlite3', '1.3.13'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.6.4'
group :development, :test do
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.0.8'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows環境ではtzinfo-dataというgemを含める必要があります
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gemfileを上記のように編集したら、以下のコマンドを実行します。
$ cd hello_app/
$ bundle install
rails serverを実行する。
$ rails server -b $IP -p $PORT
.rbファイルを編集する。
\hello_app\app\controllers\aplication_contoroller.rbを以下の通りに編集する。
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def hello
render html: "hello, world!"
end
end
\hello_app\config\route.rbを以下の通りに編集する。
Rails.application.routes.draw do
root 'application#hello'
end
Railsアプリケーションを表示する
cloud9を使用している場合は、
右上の「Share」の箇所をクリック>Link to shareのApplicationの箇所をクリック>Openをクリック