メモ > 技術 > プログラミング言語: Ruby > Ruby on Railsの導入
■Ruby on Railsの導入
■前提
>ruby -v
ruby 2.1.8p440 (2015-12-16 revision 53160) [x64-mingw32]
Ruby 2.1 がインストールされているWindows10環境で検証した
■Ruby on Railsをインストール
>gem install rails -v 5.2.2
と、Railsのバージョンを指定してインストール
バージョンを指定しないと最新版がインストールされる
■Ruby on Railsの動作確認(SQLite3)
※デフォルトではデータベースにSQLite3が使われる
https://www.oiax.jp/rails/zakkan/rails_4_2_installation_on_windows.html
この手順で動作確認
コマンドプロンプトで、Railsアプリケーションを作成したい場所へ移動
>cd C:\localhost\home\ruby\public_html\rails
>rails new rails-test --skip-bundle
>cd rails-test
>bundle install
>gem install sqlite3 -v '1.4.1' --source 'https://rubygems.org/'
>bundle install
>rails g scaffold user name:string email:string
>rake db:migrate
>rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.7-p206), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/" for ::1 at 2019-10-13 01:51:59 +0900
のように表示されれば起動完了
停止させたい場合、「Use Ctrl-C to stop」と書かれているように Ctrl-C を実行する
「rails s」は「rails server」としても起動できる
http://localhost:3000/users
にアクセスすると
「TypeError: オブジェクトでサポートされていないプロパティまたはメソッドです。」
というエラーが表示された
http://qiita.com/falcon8823/items/f4dc2b5a474869dff449
Node.jsをインストールしてからコマンドプロンプトを再起動し、再度scaffoldを実行するとエラーが解消された
名前とメールアドレスが登録管理できれば完了
Ruby - bundle install時に--skip-bundleをつけるメリット|teratail
https://teratail.com/questions/17323
■Ruby on Railsの動作確認(MySQL)
あらかじめ以下のデータベースを作成しておいた
rails-mysql_development
コマンドプロンプトで、Railsアプリケーションを作成したい場所へ移動
>cd C:\localhost\home\ruby\public_html\rails
>rails new rails-mysql -d mysql
>cd rails-mysql
rails-mysql/config/database.yml を編集(development環境がデフォルトとなり、データベースは rails-mysql_development が使われる)
password: 1234
以下を実行しておくと、各ライブラリが最新になる?
>bundle install
以下のとおり実行
>rails g scaffold user name:string email:string
>rake db:migrate
>rails s
以下にアクセスしてユーザを管理できれば完了
http://localhost:3000/users
■Railsの起動
作成済みのプログラムは、以下のようにして起動できる
>cd C:\localhost\home\ruby\public_html\rails\rails-test
>rails s
■Railsの再起動
コンソールから Ctrl+C で終了し、再度「rails s」で起動する
config/environments/staging.rb など設定ファイルで「config.cache_classes = true」としている場合、ビューを編集した場合はRailsの再起動が必要
Ctrl+C を入力すると以下のようになる
[2019-09-30 16:36:26] INFO going to shutdown ...
[2019-09-30 16:36:26] INFO WEBrick::HTTPServer#start done.
Exiting
バッチ ジョブを終了しますか (Y/N)? y
>rails s
=> Booting WEBrick
=> Rails 4.2.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2019-09-30 16:36:40] INFO WEBrick 1.3.1
[2019-09-30 16:36:40] INFO ruby 2.1.8 (2015-12-16) [x64-mingw32]
[2019-09-30 16:36:40] INFO WEBrick::HTTPServer#start: pid=8964 port=3000
■Railsのバージョンを確認
以下のようにすればRailsのバージョンを確認できる
>cd C:\localhost\home\ruby\public_html\rails\rails-test
>rails -v
Rails 4.2.2
■Railsのコンソールを起動
以下のようにすればコンソールを起動できる
この例では、合わせて環境の設定を確認している
>cd C:\localhost\home\ruby\public_html\rails\rails-test
>rails console
Loading development environment (Rails 4.2.2)
irb(main):001:0> Rails.env
=> "development"