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

質問編集履歴

1

delete

2020/04/25 09:49

投稿

rurururururu
rurururururu

スコア17

title CHANGED
File without changes
body CHANGED
@@ -1,108 +1,2 @@
1
- ### 前提・実現したいこと
2
- railsで投稿アプリを作成います。
3
-
4
- 画像を一覧表示させる機能を実装中に以下のエラーメッセージが発生しました。
5
- 以前まではこの記述で問題なく画像の表示が行えていましたが、carrierwave導入後あたりからエラーがで始めたように思います。
6
-
7
- このエラーを解消し、eachの中で画像表示させるようにしたいです。
8
- お手数ですが、お力をお貸しいただけますと幸いです。
9
- よろしくお願いいたします。
10
-
11
- ### 発生している問題・エラーメッセージ
12
-
13
- ```
14
- NoMethodError in Admin::Histories#index
15
- Showing /Users/tech-camp/projects/***/app/views/admin/histories/index.html.haml where line #20 raised:
16
-
17
- undefined method `src' for #<Image::ActiveRecord_Associations_CollectionProxy:0x00007fdcce52bea0>
18
-
19
- ```
20
-
21
- ### 該当のソースコード
22
-
23
- ```ここに言語名を入力
24
- - @histories.each do |history|
25
- .wrapper-main__parent
26
- %p.title= history.title
27
- = image_tag history.images.src.url,class:"history_image" if history.images.src.url.present?
28
- ```
29
-
30
- ### 試したこと
31
-
32
- エラーが発生し始めたのは、carrierwaveを導入してからだと考えています。
33
- 調べたところ、ActiveRecord_Associations_CollectionProxyが配列のため、番号を指定しないと表示がされないのではないかと考えています。
34
-
35
- というのも、eachの前に1番目の画像だけを常に表示させるように、下記の記述をしておりました。
36
-
37
- controller
38
- ```
39
- @history_first = History.find(1)
40
- ```
41
- view
42
- ```
43
- = image_tag @history_first.src.url,class:"history_image" if @history_first.src.url.present?
44
- ```
45
- 途中からこの記述でもエラーが出るようになってしまい、調べたところ番号を指定しなければならないとのことでした。
46
-
47
- そこで、下記の記述に変えたところ、エラーは解消されました。
48
- (そもそもコントローラーでid(1)を指定しているのですが、ActiveRecord_Associations_CollectionProxyが原因で配列になってしまっていると考えています。)
49
- ```
50
- = image_tag @history_first.images.find(1).src.url,class:"history_image" if @history_first.images.find(1).src.url.present?
51
- ```
52
-
53
- ・アソシエーションの見直し(historyとimageテーブルで以下のようにアソシエーションを組んでいます。)
54
-
55
- iamge.rb
56
- ```
57
- class Image < ApplicationRecord
58
-
59
- mount_uploader :src, ImageUploader
60
-
61
- belongs_to :cooking
62
- belongs_to :history
63
- end
64
- ```
65
-
66
- history.rb
67
- ```
68
- class History < ApplicationRecord
69
- belongs_to :user
70
- has_many :images, dependent: :destroy
71
- accepts_nested_attributes_for :images, allow_destroy: true
72
- end
73
- ```
74
-
75
- ・コントローラーの記述を確認
76
- ```
77
- def index
78
- @histories = History.includes(:user).page(params[:page]).per(5).order('created_at DESC')
79
- @history_first = History.find(1)
80
- end
81
- ```
82
-
83
- ・各値の中身を確認
84
- ```
85
- @histories
86
- #<ActiveRecord::Relation [#<History id: 4, text: "aa", title: "eee", user_id: 1, created_at: "2020-04-25 02:50:58", updated_at: "2020-04-25 02:50:58">, #<History id: 3, text: "aaa", title: "aaa", user_id: 1, created_at: "2020-04-19 04:24:02", updated_at: "2020-04-19 04:24:02">, #<History id: 2, text: "aaa", title: "eee", user_id: 1, created_at: "2020-04-19 03:58:31", updated_at: "2020-04-19 03:58:31">, #<History id: 1, text: "aaa", title: "eee", user_id: 1, created_at: "2020-04-18 15:32:30", updated_at: "2020-04-18 15:32:30">]>
87
- ```
88
-
89
- history
90
- ```
91
- #<History id: 4, text: "aa", title: "eee", user_id: 1, created_at: "2020-04-25 02:50:58", updated_at: "2020-04-25 02:50:58">
92
- ```
93
-
94
- history.images
95
- ```
96
- #<ActiveRecord::Associations::CollectionProxy [#<Image id: 4, src: "スクリーンショット_2020-04-24_19.56.35.png", cooking_id: nil, created_at: "2020-04-25 02:50:58", updated_at: "2020-04-25 02:50:58", history_id: 4>]>
97
- ```
98
-
99
- history.images.src
100
- ```
101
- NoMethodError: undefined method `src' for #<Image::ActiveRecord_Associations_CollectionProxy:0x00007fdcce52bea0>
102
- from /Users/tech-camp/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.2/lib/active_record/relation/delegation.rb:125:in `method_missing'
103
- from /Users/tech-camp/projects/kobu/app/views/admin/histories/index.html.haml:20:in `block in _app_views_admin_histories_index_html_haml__1571811841767417161_70293145854480'
104
- ```
105
-
106
- ### 補足情報(FW/ツールのバージョンなど)
107
-
108
- railsのバージョンは5.2.4.2です。
1
+ delete
2
+ 質問の仕方がよくないと思いまたので、削除たします。