LoginSignup
1
3

More than 5 years have passed since last update.

RailsのScaffoldを使わないでアプリケーションを作成してみた。No.1

Last updated at Posted at 2019-02-17

はじめに

Ruby on Railsを学び始めた時、自動で色々作成してくれるScaffoldの便利さに感動したけれど、
どういう動作をしているのか分からないので、Scaffoldを使わずにアプリケーションを作成してみる
自分用備忘録

初学者なので、気になる点がありましたらご指摘いただけますと嬉しいです

環境

Linux(Ubuntu)
Ruby 2.5.1p57
Rails 5.2.2
PostgreSQL

アプリケーションの土台を作る

ディレクトリ名はgreetingで作成

$ rails new greeting -d postgresql
$ cd greeting

データベースの設定

postgresqlを起動を起動

$  sudo service postgresql start
$ rake db:create

動作確認

http://IPアドレス:3000 にアクセス

$ rails s

コントローラークラスの作成

コントローラー名はmessagesで作成

$ rails generate controller Messages

app/controllers/messages_controller.rbを編集

app/controllers/messages_controller.rb
class MessagesController < ApplicationController
  def index
    @msg = 'こんにちは!'
  end
end

テンプレートファイルの作成

app/views/messages/の中にindex.html.erb ファイルを作成

app/views/messages/index.html.erb
<%= @msg %>

ルーティングの設定

config/routes.rb

config/routes.rb
  resources :messages

ブラウザで確認

にアクセスして、「こんにちは!」と表示されたら成功

まとめ

No.2で、モデルを作成してindexファイルを書き換えて掲示板を作成します。

RailsのScaffoldを使わないでアプリケーションを作成してみた。No.2

1
3
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
1
3