index
メソッドでは@product.adviser.user.address
が機能するのですが、show
メソッドではundefined method "address"
が出てしまいます。
*正確には、indexメソッドではに以下にて呼び出しています。
ruby
1<% @products.each do |p| %> 2<%= p.adviser.user.address %> 3<% end%>
原因わからず、誤りをご指摘頂けますと助かります。
ruby
1#show.html.erb 2<div class="container"> 3 <div class ="row"> 4 <div class="col-md-6"> 5 <p style="font-weight:bold;font-size:1.2rem;"> 6 場所:<%= @product.adviser.user.address %> 7 </p> 8 </div><!--col--> 9 </div><!--row--> 10</div><!--container --> 11
controller
は以下の通り記載しています。
ruby
1 def index 2 if params[:user_id] 3 @user = User.find(params[:user_id]) 4 @adviser = @user.adviser 5 @products = @adviser.products 6 else 7 @products = Product.all 8 end 9 @products = @products.page(params[:page]) 10 end 11 12 def show 13 @product = Product.find(params[:id]) 14 end
user
とadviser
とproduct
には以下のassociationを定義しています。
ruby
1class User < ApplicationRecord 2 has_one :adviser 3end 4 5class Adviser < ApplicationRecord 6 belongs_to :user 7 has_many :products 8end 9 10class Product < ApplicationRecord 11 belongs_to :adviser 12end
吐き出されているログは以下の通りです。
Started GET "/user/products/1" for ::1 at 2017-01-22 15:43:39 +0900 Processing by User::ProductsController#show as HTML Parameters: {"id"=>"1"} Product Load (0.3ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] Rendering user/products/show.html.erb within layouts/user Adviser Load (0.2ms) SELECT "advisers".* FROM "advisers" WHERE "advisers"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] UserImage Load (4.3ms) SELECT "user_images".* FROM "user_images" WHERE "user_images"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]] (0.4ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."product_id" = $1 [["product_id", 1]] Rendered user/products/show.html.erb within layouts/user (17.1ms) Completed 500 Internal Server Error in 24ms (ActiveRecord: 5.4ms) ActionView::Template::Error (undefined method `address' for nil:NilClass):
@product.adviser.user.address
からaddress
を抜いた@product.adviser.user
だと画面は表示されます(エラーは出ません)
ログは以下の通りです。
Started GET "/user/users/1.jpg" for ::1 at 2017-01-22 16:00:33 +0900 Processing by User::UsersController#show as JPEG Parameters: {"id"=>"1"} User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] Adviser Load (0.7ms) SELECT "advisers".* FROM "advisers" WHERE "advisers"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]] UserImage Load (11.0ms) SELECT "user_images".* FROM "user_images" WHERE "user_images"."user_id" = $1 LIMIT $2 [["user_id", 1], ["LIMIT", 1]] Rendering text template Rendered text template (0.0ms) Sent data (1.0ms) Completed 200 OK in 24ms (Views: 0.8ms | ActiveRecord: 12.4ms)
addressが空で無いこと、確認取れております。
irb(main):008:0* product = Product.find(1) Product Load (0.5ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] => #<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> irb(main):009:0> adviser = product.adviser Adviser Load (0.4ms) SELECT "advisers".* FROM "advisers" WHERE "advisers"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] => #<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"> irb(main):010:0> user = adviser.user User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]] => #<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"> irb(main):011:0> user.address => "栃木県"
binding.pry
の結果は以下の通りです。addressにしっかり入っていることが確認できました。
[1] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser => #<Adviser:0x007fda9db9a878 id: 1, user_id: 1, profile: "109でショップ店員してます!ギャル風アイメイクが得意♩", occupation: nil, created_at: Sun, 22 Jan 2017 17:25:10 JST +09:00, updated_at: Sun, 22 Jan 2017 17:25:10 JST +09:00> [2] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser.user => #<User:0x007fda9db32520 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.G37RYQe88lDpZO6d4gQWHzicVQotx4gZ37bcS2", start_date: Fri, 14 Oct 2016, end_date: nil, suspended: false, created_at: Sun, 22 Jan 2017 17:25:08 JST +09:00, updated_at: Sun, 22 Jan 2017 17:25:08 JST +09:00> [3] pry(#<#<Class:0x007fdaa123d308>>)> p.adviser.user.address => "栃木県"
@product.adviser.userはnilですか?
コメント有難う御座います。ご指摘の点、加筆しました。
「indexメソッドでは@product.adviser.user.addressが機能するのですが、showメソッドではundefined method "address"が出てしまいます」とありますが、indexメソッドの中だと@productsがあってshowだと@productがあるというのはどういうことですか?
indexでは正確には<% @products.each do |p| %> <%= p.adviser.user.address %> <% end%>にて呼び出しているのですが、しっかり表示されます
説明不足、失礼いたしました。
<% binding.pry %>を<%= p.adviser.user.address %>のあとに入れて、ターミナルで、pの値、p.adviserの値、p.adviser.userの値、p.adviser.user.addresの値を拝見してもよろしいですか?
もう一つデータベースのaddressカラムの値がnilになっていないか確認してもらってもよろしいですか?
見て頂き大変有難う御座います。<%= p.adviser.user.address %><% binding.pry %>で入れたところ、undefined method `pry' for が出てしまいました。。
debug用に、gemのpry-railsを入れてもう一度実行してみてください
addressカラムの値がnilで無いこと、質問に追記しました。
pry、知りませんでした。。基本動作のようですが、参考になりました。binding.pryの結果を質問に追記します。
<%= @product.adviser.user %><% binding.pry %>で入れて見たところ
[1] pry(#<#<Class:0x007f9356010428>>)> @product.adviser => #<Adviser:0x007f93581718b0 id: 1, user_id: nil, profile: "109でショップ店員してます!ギャル風アイメイクが得意♩", occupation: nil, created_at: Sun, 22 Jan 2017 17:25:10 JST +09:00, updated_at: Sun, 22 Jan 2017 17:25:10 JST +09:00> [2] pry(#<#<Class:0x007f9356010428>>)> @product.adviser.user => nil
nilと出ました・・・なぜuserが空になってしまうんでしょうか。。。。。
user_idがnilだからですね
なぜshowになるとadviser modelからuser_idが抜けてしまうのか、ですよね。。
外部キーの設定がおかしいか、アソシエーションがおかしいかですね。あとparamsも確認したほうがいいです
返信遅くなりました。いろいろとご指南有難う御座います!!その後、いろいろ試してみているのですが、未だ解決できず、もう少しtryします
show の view を見せてもらうことは可能ですか?
コメント有難う御座います。show.html.erbを加えました。
回答2件
あなたの回答
tips
プレビュー