質問編集履歴

2

ファイルの追加と質問の追加をしました。

2023/02/03 16:05

投稿

OHASI
OHASI

スコア96

test CHANGED
File without changes
test CHANGED
@@ -2,24 +2,20 @@
2
2
  rails初心者です。
3
3
  現在、railsチュートリアルという教材を使用して、マイクロポストに画像を表示できるようにしているのですが、次の部分に疑問を持ったので質問させていただきます。
4
4
 
5
+ ### 質問1
5
6
  例えば、以下のようなコードの場合、```<%= micropost.id %>```の```micropost```はモデル名ですか?
6
7
  そう思った理由は、画像によるとモデル名は「Micropost」でテーブル名が「microposts」だからです。
7
8
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-03/e5121065-7d1e-4659-acaa-8699681ce43e.png)
8
- ```rails
9
- # ファイル名
10
- ○○○html.erb
11
9
 
10
+ ### 質問2
12
- <li id="micropost-<%= micropost.id %>">
11
+ また、なぜコード2の```<%= render @feed_items %>```でファイル「_micropost.html.erb」(コード1)を呼び出せるのですか?
12
+
13
+ ### コード
13
- </li>
14
+ #### コード1:「以下のようなコード」の全文
14
15
  ```
15
- 以上、よろしくお願いいたします。
16
+ # ファイル名:views/microposts/_micropost.html.erb
16
17
 
17
- ---
18
- ### 編集後
19
- #### 「以下のようなコード」の全文
18
+ # ↓↓↓↓↓↓↓ここ↓↓↓↓↓↓↓
20
- ```
21
- # ファイル名:views/_microposts_icropost.html.erb
22
-
23
19
  <li id="micropost-<%= micropost.id %>">
24
20
  <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
25
21
  <span class="user"><%= link_to micropost.user.name, micropost.user %></span>
@@ -34,16 +30,83 @@
34
30
  </span>
35
31
  </li>
36
32
  ```
37
- #### 「_microposts_icropost.html.erb」を呼び出すファイル
33
+ #### コード2:コード1」を呼び出すファイル
38
34
  ```
39
35
  # ファイル名:views/shared/_feed.html.erb
40
36
 
41
37
  <% if @feed_items.any? %>
42
38
  <ol class="microposts">
39
+ # ↓↓↓↓↓↓↓ここ↓↓↓↓↓↓↓
43
40
  <%= render @feed_items %>
44
41
  </ol>
45
42
  <%= will_paginate @feed_items, params: { controller: :static_pages, action: :home } %>
46
43
  <% end %>
47
44
  ```
45
+ #### コード3:「コード2」の「@feed_items」を定義しているファイル
46
+ ```
47
+ # ファイル名:controllers/microposts_controller.rb
48
+
49
+ class MicropostsController < ApplicationController
50
+ before_action :logged_in_user, only: [:create, :destroy]
51
+
52
+ def create
53
+ @micropost = current_user.microposts.build(micropost_params)
54
+ @micropost.image.attach(params[:micropost][:image])
55
+ if @micropost.save
56
+ flash[:success] = "Micropost created!"
57
+ redirect_to root_url
58
+ else
59
+ # ↓↓↓↓↓↓↓ここ↓↓↓↓↓↓↓
60
+ @feed_items = current_user.feed.paginate(page: params[:page])
61
+ render 'static_pages/home', status: :unprocessable_entity
62
+ end
63
+ end
64
+
65
+ def destroy
66
+ end
67
+
68
+ private
69
+
70
+ def micropost_params
71
+ params.require(:micropost).permit(:content, :image)
72
+ end
73
+ end
74
+ ```
75
+ #### コード4:「コード3」の「current_user」を定義しているファイル
76
+ ```
77
+ # ファイル名:helpers/sessions_helper.rb
78
+ # 「@feed_items = current_user.feed.paginate(page: params[:page])」の「current_user」
79
+
80
+ # 記憶トークンcookieに対応するユーザーを返す
81
+ def current_user
82
+ if (user_id = session[:user_id])
83
+ @current_user ||= User.find_by(id: user_id)
84
+ elsif (user_id = cookies.encrypted[:user_id])
85
+ user = User.find_by(id: user_id)
86
+ if user && user.authenticated?(cookies[:remember_token])
87
+ log_in user
88
+ @current_user = user
89
+ end
90
+ end
91
+ end
92
+ ```
48
93
 
49
94
 
95
+ #### コード5:「コード3」の「feed」を定義しているファイル
96
+ ```
97
+ # ファイル名:models/user.rb
98
+ # 「@feed_items = current_user.feed.paginate(page: params[:page])」の「feed」
99
+
100
+ def feed
101
+ Micropost.where("user_id = ?", id)
102
+ end
103
+ ```
104
+ #### その他:User/Micropostの関連付け
105
+ ![図1](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-04/4d9a6e8c-8176-4b04-970c-e0f16dfbf769.png)
106
+
107
+ 図1 MicropostとそのUserは belongs_to(1対1)の関係性がある
108
+
109
+ ![図2](https://ddjkaamml8q8x.cloudfront.net/questions/2023-02-04/f61cdcdb-ccee-4df6-bf64-65f1e5b5922c.png)
110
+
111
+ 図2 UserとそのMicropostは has_many(1対多)の関係性がある
112
+

1

足りなかったファイルを追加をしました。

2023/02/03 15:35

投稿

OHASI
OHASI

スコア96

test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,37 @@
13
13
  </li>
14
14
  ```
15
15
  以上、よろしくお願いいたします。
16
+
17
+ ---
18
+ ### 編集後
19
+ #### 「以下のようなコード」の全文
20
+ ```
21
+ # ファイル名:views/_microposts_icropost.html.erb
22
+
23
+ <li id="micropost-<%= micropost.id %>">
24
+ <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
25
+ <span class="user"><%= link_to micropost.user.name, micropost.user %></span>
26
+ <span class="content">
27
+ <%= micropost.content %>
28
+ <% if micropost.image.attached? %>
29
+ <%= image_tag micropost.image.variant(:display) %>
30
+ <% end %>
31
+ </span>
32
+ <span class="timestamp">
33
+ Posted <%= time_ago_in_words(micropost.created_at) %> ago.
34
+ </span>
35
+ </li>
36
+ ```
37
+ #### 「_microposts_icropost.html.erb」を呼び出すファイル
38
+ ```
39
+ # ファイル名:views/shared/_feed.html.erb
40
+
41
+ <% if @feed_items.any? %>
42
+ <ol class="microposts">
43
+ <%= render @feed_items %>
44
+ </ol>
45
+ <%= will_paginate @feed_items, params: { controller: :static_pages, action: :home } %>
46
+ <% end %>
47
+ ```
48
+
49
+