質問編集履歴
4
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -70,6 +70,7 @@
|
|
70
70
|
```
|
71
71
|
/app/view/microposts/_micropost.html.erb
|
72
72
|
```ruby
|
73
|
+
<% micropost = Micropost %>
|
73
74
|
<li id="micropost-<%= micropost.id %>">
|
74
75
|
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
75
76
|
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
|
3
エラーコード記述
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,6 +9,10 @@
|
|
9
9
|
|
10
10
|
undefined method `user' for #<Class:0x00007fab78d47418>
|
11
11
|
Did you mean? super
|
12
|
+
|
13
|
+
app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200'
|
14
|
+
app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
15
|
+
app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
12
16
|
```
|
13
17
|
```ここに言語を入力
|
14
18
|
ActionView::Template::Error (undefined method `user' for #<Class:0x0000565271879460>
|
2
エラーコード記述
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,7 +10,28 @@
|
|
10
10
|
undefined method `user' for #<Class:0x00007fab78d47418>
|
11
11
|
Did you mean? super
|
12
12
|
```
|
13
|
-
|
13
|
+
```ここに言語を入力
|
14
|
+
ActionView::Template::Error (undefined method `user' for #<Class:0x0000565271879460>
|
15
|
+
Did you mean? super):
|
16
|
+
1: <% micropost = Micropost %>
|
17
|
+
2: <li id="micropost-<%= micropost.ids %>">
|
18
|
+
3: <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
19
|
+
4: <span class="user"><%= link_to micropost.user.name, micropost.user %></span>
|
20
|
+
5: <span class="content"><%= micropost.content %></span>
|
21
|
+
6: <span class="timestamp">
|
22
|
+
|
23
|
+
app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200'
|
24
|
+
app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
25
|
+
app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
|
26
|
+
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
|
27
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
|
28
|
+
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)
|
29
|
+
Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
|
30
|
+
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)
|
31
|
+
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
|
32
|
+
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)
|
33
|
+
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)
|
34
|
+
```
|
14
35
|
### 該当のソースコード
|
15
36
|
app/model/User.rb
|
16
37
|
```ruby
|
1
ソースコードを追加しました
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Rails
|
1
|
+
Rails モデル同士で紐づけをしたのにメソッドがビューで使えません
|
body
CHANGED
@@ -1,34 +1,51 @@
|
|
1
|
-
###
|
1
|
+
### 発生している問題・エラーメッセージ
|
2
2
|
|
3
|
-
|
3
|
+
・MicropostモデルとUserモデルを紐づけたが、userメソッドが使えない
|
4
|
-
|
5
|
-
|
6
|
-
### 発生している問題・エラーメッセージ
|
7
|
-
・ビューでヘルパーメソッド(userメソッド)を使うとメソッドが定義されてないというエラーが出る
|
8
|
-
・userメソッドがなぜかprivateメソッドと認識されてしまっている。
|
9
4
|
↓がエラーメッセージです。
|
10
5
|
|
11
6
|
```
|
12
7
|
NoMethodError in Microposts#index
|
8
|
+
Showing /home/ubuntu/environment/rails_app/app/views/microposts/_micropost.html.erb where line #3 raised:
|
9
|
+
|
13
|
-
|
10
|
+
undefined method `user' for #<Class:0x00007fab78d47418>
|
14
11
|
Did you mean? super
|
15
12
|
```
|
16
13
|
|
17
14
|
### 該当のソースコード
|
18
|
-
ヘルパー
|
19
|
-
```MicropostsHelper
|
20
|
-
|
21
|
-
|
15
|
+
app/model/User.rb
|
22
|
-
|
23
|
-
|
16
|
+
```ruby
|
24
|
-
|
17
|
+
class User < ApplicationRecord
|
18
|
+
has_many :microposts, dependent: :destroy
|
19
|
+
|
|
20
|
+
|
|
25
21
|
end
|
22
|
+
```
|
23
|
+
app/model/Micropost.rb
|
24
|
+
```ruby
|
25
|
+
class Micropost < ApplicationRecord
|
26
|
+
belongs_to :user
|
27
|
+
|
|
28
|
+
|
|
26
29
|
end
|
27
30
|
```
|
28
|
-
ビュー
|
29
|
-
```
|
31
|
+
```ここに言語を入力
|
32
|
+
class MicropostsController < ApplicationController
|
33
|
+
|
34
|
+
def index
|
35
|
+
@microposts=Micropost.all
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
/app/view/microposts/index.html.erb
|
40
|
+
```ruby
|
30
|
-
<%
|
41
|
+
<%@microposts.each do |post|%>
|
42
|
+
<%= render "microposts/micropost" %>
|
43
|
+
<%end%>
|
44
|
+
<%= paginate @microposts %>
|
45
|
+
```
|
46
|
+
/app/view/microposts/_micropost.html.erb
|
47
|
+
```ruby
|
31
|
-
<li id="micropost-<%= micropost.
|
48
|
+
<li id="micropost-<%= micropost.id %>">
|
32
49
|
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
|
33
50
|
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
|
34
51
|
<span class="content"><%= micropost.content %></span>
|
@@ -37,12 +54,12 @@
|
|
37
54
|
</span>
|
38
55
|
</li>
|
39
56
|
```
|
57
|
+
|
40
58
|
### 試したこと
|
41
59
|
|
42
60
|
binding.pryを使ってコンソール内でいくつか検証してみました。
|
43
61
|
`Micropost.first.user`⇒ユーザー参照できました。
|
44
62
|
`micropost.first.user`も同様です。
|
45
|
-
また、privateメソッドにならないようにpublicを挟んでみましたが効果はなかったです。
|
46
63
|
|
47
64
|
|
48
65
|
### 補足情報(FW/ツールのバージョンなど)
|