Return to Tech/ruby

Ruby on Rails


Railsアプリケーションの作成

$ rails new webapps -d mysql
# Railsアプリケーション名は webapps
# 利用するデータベースはMySQL

上記コマンド完了後、webappsというディレクトリが作成されます。
ディレクトリ、ファイル構成は以下のようになります。

Gemfileファイル
Gemfile.lockファイル
README.rdocファイル
Rakefileファイル
appディレクトリ
binディレクトリ
configディレクトリ
config.ruファイル
dbディレクトリ
libディレクトリ
publicディレクトリ
tmpディレクトリ
vendorディレクトリ

参考情報gem list


データベース関連の設定 データベースに関する設定は下記のファイルを編集し 適用します。 ここでの例 rootでpasswordはhogehoge ローカルホストのMySQLサーバへ接続します 赤文字部分が調整箇所となります。 file:webapps/config/database.yml # MySQL. Versions 5.0+ are recommended. # # Install the MYSQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html # default: &default adapter: mysql2 encoding: utf8 pool: 5 username: root
password: hogehoge
socket: /var/lib/mysql/mysql.sock development: <<: *default database: webapps_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this do to the same as development or production. test: <<: *default database: webapps_test # As with config/secrets.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password as a unix enviornment variable when you root # the app. Read http://guides rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variable in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment vairalbe. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: webapps_production username: webapps password: <%= ENV['WEBAPPS_DATABASE_PASSWORD'] %>
データベースの設定 データベース作成 webappsディレクトリ下で以下を実行します。 $ bin/rake db:create エラー、警告等のメッセージが出力されなければ MySQLサーバにデータベースwebapps_developmentが作成されていることを確認できます。 MariaDB [(none)]> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema |
| webapps_development |
+---------------------
Railsアプリケーションの起動テスト 内部のサーバ機能(WEBrick)を利用し Railsアプリケーションの起動確認を行います。 $ rails server --bind=192.168.11.20 or $ rails s --bind-192.168.11.20 起動完了後、以下のような出力結果になります。 $ rails server --bind=192.168.11.20 => Booting WEBrick => Rails 4.2.5 application starting in development on http://192.168.11.20:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [YYYY-MM-DD HH:mm:ss] INFO WEBrick 1.3.1 [YYYY-MM-DD HH:mm:ss] INFO ruby 2.2.4 (2015-12-16) [x86_64-linux] [YYYY-MM-DD HH:mm:ss] INFO WEBrick::HTTPServer#start: pid=28433 port=3000 http://192.168.11.20:3000/にアクセスすると以下のような 出力結果となります Started GET "/": for 192.168.11.20 at YYYY-MM-DD HH:mm:ss +0900 Cannot render console from 192.168.11.20! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Rails::WelcomeController#index as HTML Rendered /usr/local/lib/ruby/gems/2.2.0/gems/railties-4.2.5/lib/rails/templates/rails/welcome/index.html.erb (3.8ms) Completed 200 OK in 64ms (Views: 36.3ms | ActiveRecord: 0.0ms) 以下、接続イメージとなります。

Return to Tech/ruby