teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

show追加

2017/01/27 12:41

投稿

innjera
innjera

スコア132

title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,21 @@
9
9
 
10
10
  原因わからず、誤りをご指摘頂けますと助かります。
11
11
 
12
+ ```ruby
13
+ #show.html.erb
14
+ <div class="container">
15
+ <div class ="row">
16
+ <div class="col-md-6">
17
+ <p style="font-weight:bold;font-size:1.2rem;">
18
+ 場所:<%= @product.adviser.user.address %>
19
+ </p>
20
+ </div><!--col-->
21
+ </div><!--row-->
22
+ </div><!--container -->
23
+
24
+ ```
25
+
26
+
12
27
  `controller`は以下の通り記載しています。
13
28
  ```ruby
14
29
  def index

4

修正

2017/01/27 12:41

投稿

innjera
innjera

スコア132

title CHANGED
File without changes
body CHANGED
@@ -92,4 +92,37 @@
92
92
  irb(main):011:0> user.address
93
93
  => "栃木県"
94
94
 
95
+ ```
96
+
97
+ `binding.pry`の結果は以下の通りです。addressにしっかり入っていることが確認できました。
98
+ ```
99
+ [1] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser
100
+ => #<Adviser:0x007fda9db9a878
101
+ id: 1,
102
+ user_id: 1,
103
+ profile: "109でショップ店員してます!ギャル風アイメイクが得意♩",
104
+ occupation: nil,
105
+ created_at: Sun, 22 Jan 2017 17:25:10 JST +09:00,
106
+ updated_at: Sun, 22 Jan 2017 17:25:10 JST +09:00>
107
+ [2] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser.user
108
+ => #<User:0x007fda9db32520
109
+ id: 1,
110
+ email: "yuu.sato@example.com",
111
+ email_for_index: "yuu.sato@example.com",
112
+ family_name: "佐藤",
113
+ given_name: "悠",
114
+ family_name_kana: "sato",
115
+ given_name_kana: "yuu",
116
+ user_name: "mitsuo",
117
+ address: "栃木県",
118
+ hashed_password:
119
+ "$2a$10$h2IbGgkaimCUYM.G37RYQe88lDpZO6d4gQWHzicVQotx4gZ37bcS2",
120
+ start_date: Fri, 14 Oct 2016,
121
+ end_date: nil,
122
+ suspended: false,
123
+ created_at: Sun, 22 Jan 2017 17:25:08 JST +09:00,
124
+ updated_at: Sun, 22 Jan 2017 17:25:08 JST +09:00>
125
+ [3] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser.user.address
126
+ => "栃木県"
127
+
95
128
  ```

3

追加

2017/01/22 09:14

投稿

innjera
innjera

スコア132

title CHANGED
File without changes
body CHANGED
@@ -76,4 +76,20 @@
76
76
  Rendered text template (0.0ms)
77
77
  Sent data (1.0ms)
78
78
  Completed 200 OK in 24ms (Views: 0.8ms | ActiveRecord: 12.4ms)
79
+ ```
80
+
81
+ addressが空で無いこと、確認取れております。
82
+ ```ここに言語を入力
83
+ irb(main):008:0* product = Product.find(1)
84
+ Product Load (0.5ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
85
+ => #<Product id: 1, adviser_id: 1, price: 2000, required_time: 1, title: "ファンデーションの基礎教えます", parts: "チーク", situation: "仕事", makeupkit: "自分の道具不要(アドバイザーの物を使用)", makeupkit_explain: "おすすめの道具等お教えします。", memo: "就職活動で重要な知的さと清楚さを第一印象として与える出すメイク教えます。で買いました。", place: "東京都内ならどこでも", place_memo: "山下公園のベンチを考えています。", posted_at: "2017-01-13 08:25:09", data: nil, created_at: "2017-01-22 08:25:09", updated_at: "2017-01-22 08:25:09", makeimg: nil, makeimgs: [], makeimg_two: nil, makeimg_three: nil>
86
+ irb(main):009:0> adviser = product.adviser
87
+ Adviser Load (0.4ms) SELECT "advisers".* FROM "advisers" WHERE "advisers"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
88
+ => #<Adviser id: 1, user_id: 1, profile: "109でショップ店員してます!ギャル風アイメイクが得意♩", occupation: nil, created_at: "2017-01-22 08:25:10", updated_at: "2017-01-22 08:25:10">
89
+ irb(main):010:0> user = adviser.user
90
+ User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
91
+ => #<User id: 1, email: "yuu.sato@example.com", email_for_index: "yuu.sato@example.com", family_name: "佐藤", given_name: "悠", family_name_kana: "sato", given_name_kana: "yuu", user_name: "mitsuo", address: "栃木県", hashed_password: "$2a$10$h2IbGgkaimCUYM.G37RYQe88lDpZO6d4gQWHzicVQot...", start_date: "2016-10-14", end_date: nil, suspended: false, created_at: "2017-01-22 08:25:08", updated_at: "2017-01-22 08:25:08">
92
+ irb(main):011:0> user.address
93
+ => "栃木県"
94
+
79
95
  ```

2

修正

2017/01/22 08:52

投稿

innjera
innjera

スコア132

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,12 @@
1
1
  `index`メソッドでは`@product.adviser.user.address`が機能するのですが、`show`メソッドでは`undefined method "address"`が出てしまいます。
2
2
 
3
+ *正確には、indexメソッドではに以下にて呼び出しています。
4
+ ```ruby
5
+ <% @products.each do |p| %>
6
+ <%= p.adviser.user.address %>
7
+ <% end%>
8
+ ```
9
+
3
10
  原因わからず、誤りをご指摘頂けますと助かります。
4
11
 
5
12
  `controller`は以下の通り記載しています。

1

修正

2017/01/22 08:00

投稿

innjera
innjera

スコア132

title CHANGED
File without changes
body CHANGED
@@ -53,4 +53,20 @@
53
53
 
54
54
 
55
55
  ActionView::Template::Error (undefined method `address' for nil:NilClass):
56
+ ```
57
+
58
+ `@product.adviser.user.address`から`address`を抜いた`@product.adviser.user`だと画面は表示されます(エラーは出ません)
59
+ ログは以下の通りです。
60
+
61
+ ```ここに言語を入力
62
+ Started GET "/user/users/1.jpg" for ::1 at 2017-01-22 16:00:33 +0900
63
+ Processing by User::UsersController#show as JPEG
64
+ Parameters: {"id"=>"1"}
65
+ User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
66
+ Adviser Load (0.7ms) SELECT "advisers".* FROM "advisers" WHERE "advisers"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]]
67
+ UserImage Load (11.0ms) SELECT "user_images".* FROM "user_images" WHERE "user_images"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]]
68
+ Rendering text template
69
+ Rendered text template (0.0ms)
70
+ Sent data (1.0ms)
71
+ Completed 200 OK in 24ms (Views: 0.8ms | ActiveRecord: 12.4ms)
56
72
  ```