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?

エラー(未解決) NoMethodError in Reserves#index

Last updated at Posted at 2024-12-05

はじめに

ありふれたアプリですが勉強として床屋(TOKOYA)の予約アプリを作っています。
実行したら以下のようなエラーが出ました。

NoMethodError in Reserves#index
Showing /home/projects/tokoya_reserve/app/views/reserves/index.html.erb where line #2 raised:

undefined method `datetime' for #<ActiveRecord::Relation []>
Extracted source (around line #2):
1 <h1>TOKOYA</h1>
2 <%= @reserves.datetime %>
3 <%= @reserves.name %>
4 <%= @reserves.course%>
app/views/reserves/index.html.erb 訂正前
<h1>TOKOYA</h1>
<%= @reserves.datetime %>
<%= @reserves.name %>
<%= @reserves.course%>

上記のビューでは、@reservesを単一のオブジェクトとして扱っているが@reservesが配列(複数のレコード)を含む場合、繰り返し処理が必要なので以下に訂正が必要。

app/views/reserves/index.html.erb 訂正後
<h1>TOKOYA</h1>
<% @reserves.each do |reserve| %>
  <p>日時: <%= reserve.datetime %></p>
  <p>名前: <%= reserve.name %></p>
  <p>コース: <%= reserve.course %></p>
  <hr>
<% end %>

おわりに

とりあえず上記までやって、明日以降ルーティング設定をしたいと思います。

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?