0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ビューの基本

Posted at

ERB(Embedded Ruby)という形式。
HTMLにRubyを埋め込んだもの。
最終的なイメージを描きながら作業できる。

プログラム側で必要な情報を用意(Controller)

テンプレート側では、
データを埋め込む場所や表示方法を定義。(View)

これがController&Viewの基本的な関係

@msg = ''
インスタンス変数

動的な処理は
<% %>か<%= %>
で記述する。

<% %>はコードを実行するのみ
<%= %>は中身の式の結果を出力する

テンプレートファイルがなんらかの出力を行うという役割を
しっかりと意識するならばほとんどは
<%= %>で記述するでしょう。

空白の制御
<% -%>,<%= -%>のように、-で閉じると-%>の後方の
スペースや改行を除去することもできます。

ビューの自動生成:
コントローラークラスと合わせて、ビューも自動生成できる。

rails generate controller hello show
= helloコントローラのshow.html.erbを生成

rails generate controller hello index show new
= index.html.erb, show.html.erb, new.html.erbを生成
あらかじめ必要なファイルが分かっている場合、まとめて生成する方が良い。

アクションメソッドは省略可
e.g. http://localhost:3000/hello/nothing
Railsはhelloコントローラのnothingアクションを探すが、無い場合そのままhello/nothing.html.erbを健作氏、実行します。

共通レイアウトの適用:
共通要素はapplication.html.erbで定義されている。
その中<%= yield %>をreplaceして出力する。

コメントアウト:
Rubyのコメント構文を使う!
<%# コメント %>

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?