LoginSignup
0
0

More than 3 years have passed since last update.

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

Posted at

アプリケーションの作成

command
% rails _6.0.0_ new application -d mysql

・「6.0.0」でバージョンを指定しています。
・「application」は作成したいアプリケーション名を示します。
・「- d mysql」のオプションをつけることで、
  データ管理ツールとしてMYSQLを使用します。

データベースの作成

config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock

「utf8」のようにエンコーディングの設定を行うことができます。

command
% rails db:create 

railsコマンドでデータベースを作成。

ルーティングの設定

config/route.rb
Rails.application.routes.draw do
  root to: "notes#index"
end

ルートパスへのアクセスがあったら、notes_controllerのindexアクションが呼び出されるようになる。

ビューとコントローラーの設定

config/application.rb
# 省略
 config.load_defaults 6.0
# 中略
 config.generators do |g|
   g.stylesheets false
   g.javascripts false
   g.helper false
   g.test_framework false
 end

rails gコマンドでコントローラーを作成する前に、必要のないファイルを生成しないように設定。

commnad
% rails g controller notes index

コントローラー作成時にコントローラ名に続けてアクション名を指定すると、
・notesコントローラーにindexアクションが作られる
・viewsのnotesフォルダにindex.html.erbファイルが作られる

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