LoginSignup
17
18

More than 5 years have passed since last update.

Ruby on Rails を始める

Last updated at Posted at 2014-04-13

Ruby on Railsを始める

1. 前置き

1. 前提・事前作業


2. #03 はじめてのRuby on Rails

2-1. railsアプリケーション作成

  • railsコマンドを実行(カレントディレクトリにザクザクディレクトリを作っていくっぽい)
rails new myApp
  • run bundle install というのが実行された状態で結構長い間処理が止まったままになる。
  • ライブラリとかをインストールしてるらしい

  • 終わったららcd myAppでディレクトリ移動

  • サーバを起動(serverのエイリアスでsでもOK)

rails server
or
rails s
  • エラーになる
/home/vagrant/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/execjs-2.0.2/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
  • javascriptのruntimeが見つからないとか何とか。
  • Gemfileを編集してエラーを解消(アプリケーションのルートディレクトリにいる)
vi Gemfile
  • 下記コメントアウトされてるのを有効化
# gem 'therubyracer',  platforms: :ruby
  • 再度必要なものをインストール(?)
bundle install
  • installing therubyracer 0.12.1とか出てたので、確かに追加インストールされたっぽい。
  • 改めて
rails s
  • 起動した(以下、ターミナルに表示された内容。サーバ監視モード(?)になって入力は受け付けなくなるっぽい)
=> Booting WEBrick
=> Rails 4.1.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-04-13 08:19:17] INFO  WEBrick 1.3.1
[2014-04-13 08:19:17] INFO  ruby 2.1.1 (2014-02-24) [x86_64-linux]
[2014-04-13 08:19:17] INFO  WEBrick::HTTPServer#start: pid=8720 port=3000
  • ポート3000にホストOSからアクセスしてみる(ホストOSのhostsで`192.168.33.56 dev.com'としてあるので、下記URLにアクセス

無題.jpg

  • こんな画面が出る(構築はここまで)
  • myAppの配下の以下のディレクトリを主に弄ることになる。
    • app
    • config
    • db

3. #04 Scaffoldを使ってみよう

3-1. Scaffoldとは

  • コードを自動生成するジェネレータ
  • rails コマンドの一部

3-2. 生成してみる

  • rails s でサーバを起動したままだと入力できないので、Ctrl + C で一旦サーバをストップさせたうえで実行
rails generate scaffold User name:string score:integer
  • こんなメッセージが
create    db/migrate/20140413085337_create_users.rb
  • DBに設定を反映させるためのスクリプトのようなものらしい
  • rakeコマンドで設定を反映させる
rake db:migrate

無題.jpg

  • New Userリンクを押下

無題.jpg

  • これでユーザを登録できるっぽい。
17
18
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
17
18