質問編集履歴
4
変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -142,6 +142,8 @@
|
|
142
142
|
|
143
143
|
```ruby
|
144
144
|
|
145
|
+
<% micropost = Micropost %>
|
146
|
+
|
145
147
|
<li id="micropost-<%= micropost.id %>">
|
146
148
|
|
147
149
|
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
3
エラーコード記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -19,6 +19,14 @@
|
|
19
19
|
undefined method `user' for #<Class:0x00007fab78d47418>
|
20
20
|
|
21
21
|
Did you mean? super
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200'
|
26
|
+
|
27
|
+
app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
28
|
+
|
29
|
+
app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
22
30
|
|
23
31
|
```
|
24
32
|
|
2
エラーコード記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,7 +22,49 @@
|
|
22
22
|
|
23
23
|
```
|
24
24
|
|
25
|
+
```ここに言語を入力
|
25
26
|
|
27
|
+
ActionView::Template::Error (undefined method `user' for #<Class:0x0000565271879460>
|
28
|
+
|
29
|
+
Did you mean? super):
|
30
|
+
|
31
|
+
1: <% micropost = Micropost %>
|
32
|
+
|
33
|
+
2: <li id="micropost-<%= micropost.ids %>">
|
34
|
+
|
35
|
+
3: <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
36
|
+
|
37
|
+
4: <span class="user"><%= link_to micropost.user.name, micropost.user %></span>
|
38
|
+
|
39
|
+
5: <span class="content"><%= micropost.content %></span>
|
40
|
+
|
41
|
+
6: <span class="timestamp">
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200'
|
46
|
+
|
47
|
+
app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
48
|
+
|
49
|
+
app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
50
|
+
|
51
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
|
52
|
+
|
53
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
|
54
|
+
|
55
|
+
Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.8ms)
|
56
|
+
|
57
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
|
58
|
+
|
59
|
+
Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms)
|
60
|
+
|
61
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
|
62
|
+
|
63
|
+
Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
|
64
|
+
|
65
|
+
Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (17.7ms)
|
66
|
+
|
67
|
+
```
|
26
68
|
|
27
69
|
### 該当のソースコード
|
28
70
|
|
1
ソースコードを追加しました
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Rails
|
1
|
+
Rails モデル同士で紐づけをしたのにメソッドがビューで使えません
|
test
CHANGED
@@ -1,18 +1,8 @@
|
|
1
|
-
###
|
1
|
+
### 発生している問題・エラーメッセージ
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
・MicropostモデルとUserモデルを紐づけたが、userメソッドが使えない
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
### 発生している問題・エラーメッセージ
|
12
|
-
|
13
|
-
・ビューでヘルパーメソッド(userメソッド)を使うとメソッドが定義されてないというエラーが出る
|
14
|
-
|
15
|
-
・userメソッドがなぜかprivateメソッドと認識されてしまっている。
|
16
6
|
|
17
7
|
↓がエラーメッセージです。
|
18
8
|
|
@@ -22,7 +12,11 @@
|
|
22
12
|
|
23
13
|
NoMethodError in Microposts#index
|
24
14
|
|
15
|
+
Showing /home/ubuntu/environment/rails_app/app/views/microposts/_micropost.html.erb where line #3 raised:
|
16
|
+
|
17
|
+
|
18
|
+
|
25
|
-
|
19
|
+
undefined method `user' for #<Class:0x00007fab78d47418>
|
26
20
|
|
27
21
|
Did you mean? super
|
28
22
|
|
@@ -32,33 +26,73 @@
|
|
32
26
|
|
33
27
|
### 該当のソースコード
|
34
28
|
|
35
|
-
|
29
|
+
app/model/User.rb
|
36
30
|
|
37
|
-
```
|
31
|
+
```ruby
|
38
32
|
|
33
|
+
class User < ApplicationRecord
|
39
34
|
|
35
|
+
has_many :microposts, dependent: :destroy
|
40
36
|
|
41
|
-
|
37
|
+
|
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
def user
|
46
|
-
|
47
|
-
return User.find_by(id: self.user_id)
|
48
|
-
|
49
|
-
|
39
|
+
|
|
50
40
|
|
51
41
|
end
|
52
42
|
|
53
43
|
```
|
54
44
|
|
55
|
-
|
45
|
+
app/model/Micropost.rb
|
56
46
|
|
57
|
-
```
|
47
|
+
```ruby
|
58
48
|
|
59
|
-
|
49
|
+
class Micropost < ApplicationRecord
|
60
50
|
|
51
|
+
belongs_to :user
|
52
|
+
|
53
|
+
|
|
54
|
+
|
55
|
+
|
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
```ここに言語を入力
|
62
|
+
|
63
|
+
class MicropostsController < ApplicationController
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
def index
|
68
|
+
|
69
|
+
@microposts=Micropost.all
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
/app/view/microposts/index.html.erb
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
|
81
|
+
<%@microposts.each do |post|%>
|
82
|
+
|
83
|
+
<%= render "microposts/micropost" %>
|
84
|
+
|
85
|
+
<%end%>
|
86
|
+
|
87
|
+
<%= paginate @microposts %>
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
/app/view/microposts/_micropost.html.erb
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
|
61
|
-
<li id="micropost-<%= micropost.id
|
95
|
+
<li id="micropost-<%= micropost.id %>">
|
62
96
|
|
63
97
|
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
64
98
|
|
@@ -76,6 +110,8 @@
|
|
76
110
|
|
77
111
|
```
|
78
112
|
|
113
|
+
|
114
|
+
|
79
115
|
### 試したこと
|
80
116
|
|
81
117
|
|
@@ -85,8 +121,6 @@
|
|
85
121
|
`Micropost.first.user`⇒ユーザー参照できました。
|
86
122
|
|
87
123
|
`micropost.first.user`も同様です。
|
88
|
-
|
89
|
-
また、privateメソッドにならないようにpublicを挟んでみましたが効果はなかったです。
|
90
124
|
|
91
125
|
|
92
126
|
|