LoginSignup
12
14

More than 5 years have passed since last update.

Ruby + bundler + Rails => Hello World

Posted at

メモの初めに

  • Railsアプリケーションの基本的な初期状態を作りたい。
  • bundlerでアプリケーションごとに環境作って運用したい。
  • passengerを使ってapache越しにアクセスできるようにしたい(アプリケーションの起動をhttpd startに任せたい)。

構築

インストール

Ruby、RubyGemsのインストールは省略。

sudo ruby --version

ができるように ~/.bashrc に以下を追加(すでにできてるなら不要)。

alias sudo='sudo env PATH=$PATH'

bundlerをインストール。

sudo gem install bundler

アプリのディレクトリを作成してinit。

mkdir /var/www/helloworld
cd /var/www/helloworld
bundle init

生成されたGemfileを編集。

# A sample Gemfile
source "https://rubygems.org"

gem "rails", "3.2.13"

このディレクトリ以下にインストール。

bundle install --path vendor/bundle
bundle exec rails new .

途中、Gemfileを上書きするか聞いてくるので気にせず上書き。
execjsなども追加して最終的にGemfileは以下のようになる。

source 'https://rubygems.org'

gem 'rails', '3.2.13'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'
gem 'execjs'
gem 'therubyracer'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

編集後もう一回インストールして、コントローラーなどを生成する。
そしてWEBrickサーバー起動。

bundle install --path vendor/bundle
bundle exec rails generate controller Hello index
bundle exec rails s

http://IPアドレス:3000/hello/index
でページが表示される。

アプリケーションに手を加える

メッセージの表示。
以下のようにテキストを出力できる。

app/controllers/hello_controller.rb
class HelloController < ApplicationController
  def index
    render text: "Hello World!"
  end
end

また、「/hello/to/ミスター」のようなパスにアクセスした際に、「Hello ミスター !」とビューファイルを使用して出力する場合。

ルートを設定し、

config/routes.rb
Hellowrold::Application.routes.draw do
  get "hello/index"

  get "hello/to/:name" => "hello#to"
end

ビューに渡すメッセージを登録し、

app/controllers/hello_controller.rb
class HelloController < ApplicationController
  def index
    render text: "Hello World!"
  end

  def to
    @message = "Hello #{params[:name]} !"
  end
end

ビューで出力する。

app/views/hello/to.html.erb
<p><%= @message.html_safe %></p>

app/views/hello/to.html.erb は、レイアウトファイルである app/views/layouts/application.html.erb の中に組み込まれる形で出力される。

passengerを使う

passengerを使ってapache越しにアクセスできるようにする。
(libcurlやらg++やらhttpd-develやら必要な場合は適宜インストール、省略)

sudo gem install passenger
sudo passenger-install-apache2-module
vi /etc/httpd/conf.d/passenger.conf

内容は次のようになる。

LoadModule passenger_module /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/2.0.0/gems/passenger-4.0.5
PassengerDefaultRuby /usr/local/bin/ruby

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /var/www/hellowrold/public
    ServerName ホスト
    ErrorLog logs/helloworld-error_log
    CustomLog logs/helloworld-access_log common
    RackEnv development
    RackBaseURI /
    SetEnv GEM_HOME /var/www/hellowrold/vendor/bundle/ruby/2.0.0
    <Directory "/var/www/hellowrold/public">
      Order allow,deny
      Allow from all
    </Directory>
</VirtualHost>

LoadModuleの行からPassengerDefaultRubyの行は、passenger-install-apache2-moduleの最後に出てくる情報を使う。

http://ホスト/hello/index
http://ホスト/hello/to/YOURNAME
でアクセスできる。

12
14
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
12
14