1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【初心者向け】Ruby on Rails webサイトへの表示について

Last updated at Posted at 2024-09-25

現在、デイトラでWeb開発を学んでおり、その学習において、学んだことを共有させていただきます!

現在は、Ruby on RailsでWebサイトを作成中です。

スクリーンショット 2024-09-25 15.09.10.png

どうやってこれらの文字を表示しているのでしょうか?

routes.rb
Rails.application.routes.draw do
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  get '/' => 'home#index'
end

ブラウザから get '/' リクエストがきたら、routes.rbで処理が開始され、home#indexが実行されます。

home#index が実行されると、home_controller.rb 内のindexメソッドが処理を始めます。

home_controller.rb
class HomeController < ApplicationController
	def index
        render 'home/index'
		# render 'home/index' この部分は省略することができる。renderは「表示する」の意味
		@title = 'デイトラ'
        # @title はインスタンス変数
	end
end

indexメソッドが実行されると、titleに代入した文字列'デイトラ'が表示されます。

index.html.erb
<h1>HOME</h1>
<h2><%= @title %></h2>

ちなみに、h1タグ("HOME")については、通常のHTMLですが、その下のh2タグは違います。

<h2><%= @title %></h2>

このコードが実行されると、title(インスタンス変数)の内容がh2タグの中に挿入されてHTMLとして出力されます。

結果として...

スクリーンショット 2024-09-25 15.09.10.png

このようにブラウザ上で表示されることになります。

これからも学んだことを随時アウトプットしていきます!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?