LoginSignup
0
0

More than 5 years have passed since last update.

【勉強メモ】テーブルのデータをviewに表示させる【挫折しない!】

Posted at

1.controllerにテーブルデータの変数を代入

app/controller/test_controller.rb
  def index
    @memos = Memo.all
  end

2.viewをいじる

何度書いても忘れるから書いておく。

@memos each.do |memo|

each.doの繰り返し。

app/view/test/index.html.erb
<h1>Test#index</h1>
<p>Find me in app/views/test/index.html.erb</p>
<center>
<table border="1">
<tr><th>名前</th><th>性別</th><th>メモ</th></tr>
<% @memos.each do |memo| %>
<tr><td><center><%= memo.name %></center></td>
<td><center><%= memo.sex %></center></td>
<td><center><%= memo.memo %></center></td>
<% end %>

3.ビューで表示させる。

スクリーンショット 2017-10-09 18.45.57.png

やった!!

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