質問編集履歴
1
モデル、shemaファイルの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,7 +10,8 @@
|
|
10
10
|
**子モデル:_item_image_**
|
11
11
|
|
12
12
|
### 実際の動作
|
13
|
+
itemを一覧表示させようとindexページにeach文でひとつずつ取り出しているのですが、item_image.imageがnilの場合の処理がなにも行われません。
|
13
|
-
item_image.image
|
14
|
+
item_image.image.nil?で画像がない場合の処理を先に書いてもelseの処理は行われますが、nilの処理を行うことができず、ブラウザもなにもないことになっています。
|
14
15
|
|
15
16
|
nil blank presentなどを試してもダメだったので、これは親モデルのeach文内で子モデルのカラムのnilを判別できないなど子モデルのデータの取得になにか問題があるのでしょうか?
|
16
17
|
|
@@ -46,5 +47,41 @@
|
|
46
47
|
</div>
|
47
48
|
</div>
|
48
49
|
```
|
50
|
+
**schema.rb**
|
51
|
+
```ruby
|
52
|
+
create_table "item_images", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
53
|
+
t.string "image"
|
54
|
+
t.integer "item_id"
|
55
|
+
t.datetime "created_at", precision: 6, null: false
|
56
|
+
t.datetime "updated_at", precision: 6, null: false
|
57
|
+
end
|
58
|
+
|
59
|
+
create_table "items", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
|
60
|
+
t.string "title", null: false
|
61
|
+
t.text "explain", null: false
|
62
|
+
t.integer "price", null: false
|
63
|
+
t.bigint "saler_id"
|
64
|
+
t.bigint "buyer_id"
|
65
|
+
t.datetime "created_at", precision: 6, null: false
|
66
|
+
t.datetime "updated_at", precision: 6, null: false
|
67
|
+
t.bigint "user_id"
|
68
|
+
end
|
69
|
+
```
|
70
|
+
**item.rb**
|
71
|
+
```ruby
|
72
|
+
class Item < ApplicationRecord
|
73
|
+
belongs_to :user
|
74
|
+
has_many :item_images, dependent: :destroy
|
75
|
+
|
76
|
+
accepts_nested_attributes_for :item_images, allow_destroy: true
|
77
|
+
end
|
78
|
+
```
|
79
|
+
**item_image.rb**
|
80
|
+
```ruby
|
81
|
+
class ItemImage < ApplicationRecord
|
82
|
+
belongs_to :item
|
83
|
+
mount_uploader :image, ImageUploader
|
84
|
+
end
|
85
|
+
```
|
49
86
|
|
50
87
|
何卒よろしくお願いします。
|