LoginSignup
0
0

More than 3 years have passed since last update.

rails 基本その1 基本作成

Last updated at Posted at 2019-11-05

要所での内容がほとんどなので
ここで基本に立ち返って、railsの立ち上げ基本について書こうと思います。

※railsの導入がされてること前提です。
※projectsディレクトリに導入されているとします
※導入例としてhome/indexを作成します。

その1,rails導入ディレクトリに移動

ターミナル
 
  # 「projects」ディレクトリに移動 
  $ cd projects

  # 現在のディレクトリのパスを表示して現在のパスを確認
  $ pwd

その2,アプリケーションとデータベースを作成

ターミナル
 $ rails new アプリケーション名
  # アプリケーションを新規作成

  $ rails new アプリケーション名 -オプション名
  # オプションを付けてアプリケーションを作成

  $ cd アプリケーション名
  # アプリケーション名のディレクトリに移動

   #データベースの作成
   $ rake db:create
   #エラーが出る場合は
  $ bundle exec rake db:create

  $ pwd
  # 現在のディレクトリのパスを表示

作成例
  $ rails _5.2.2.1_ new test -d mysql
  # 「test」を「mysql」オプションで作成。バージョンを5.2.2.1で作成。

その3,ルーティング(アドレス)の設定

config/routes.rb
 #home/indexでページに飛べるように設定します。 
  Rails.application.routes.draw do
    get 'home' => 'home#index'
  end

その4,コントローラーの作成(任意のデータ抽出、並び替えをここで行う)

ターミナル
 $ rails g controller コントローラ名
  # コントローラを作成
app/controllers/home_controller.rb
 class homeController < ApplicationController

    def index
    end

  end
  # アクションの作成

その5,ビューの作成(ここがないと始まらない)

app/viewsに
homeディレクトリを作成しindex.htmlを作成します。
試しに下記のようなコマンドを打ち込んでください。

app/views/home/index.html
 $ <h1>Hello world</h1>
  # プログラミングの超基本コマンドです。

その6,立ち上げ

ターミナル
 $ rails s
  # これで立ち上がります。

あとはプラウザで
http://localhost:3000/home
で表示されれば完成です。

0
0
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
0
0