13
10

More than 5 years have passed since last update.

Ruby on RailsでHelloWorld!を表示しました

Last updated at Posted at 2014-11-08

Ruby on RailsでHelloWorld!を表示しました。モデルの無いコントローラとビューだけの構成です。プロジェクトはすでに生成されているところからスタートしました。

コントローラを追加

検索のコントローラということでsearchコントローラを追加します。

検索コントローラを追加
bundle exec rails g controller search

search_contrller.rbが生成されます。

アクションを追加

app/controllers/search_controller.rbにアクションを記述します。

helloworldアクションを追加
class SearchController < ApplicationController
  def helloworld
  end
end

helloworldアクションを追加しました。

ビューを追加

helloworldアクションに対してhelloworld.html.erbを追加します。

ビューファイルを追加
vi app/views/search/helloworld.html.erb
erbにHelloWorldを記述
<h1>HelloWorld!</h1>

HelloWorld!を書きました。

ルーティングを設定

config/routes.rbを編集します。

Rails.application.routes.draw do
  get 'helloworld' => 'search#helloworld'
end

元からroutes.rbにサンプルがあるので、それを真似して書くと何とかなります。

サーバーを起動

サーバを起動します。

bundle exec rails s

ブラウザから動作を確認

ブラウザから http://localhost:3000/helloworld へアクセスして動作を確認します。「HelloWorld!」と表示されていれば成功です。

感想

RailsでHelloWorld!を表示できました。
HelloWorld!を表示するまでのステップが多い気がするのですが、フレームワークだとこんなものなんでしょうか(^_^;)

13
10
2

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
13
10