質問編集履歴
4
情報追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -42,7 +42,10 @@
|
|
42
42
|
accepts_nested_attributes_for :lesson_images, allow_destroy: true
|
43
43
|
|
44
44
|
#LessonImage Model
|
45
|
+
class LessonImage < ApplicationRecord
|
45
|
-
|
46
|
+
belongs_to :lesson, optional: true(#Rails 5.0.0.1を利用)
|
47
|
+
mount_uploader :lesson_image, LessonImageUploader
|
48
|
+
end
|
46
49
|
|
47
50
|
#LessonImageのテーブル
|
48
51
|
# Table name: lesson_images
|
3
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
出てしまうエラー
|
9
9
|
---
|
10
|
-
|
10
|
+
undefined method `url' for #<LessonImage:0x007fdeb2b92c68>`といったエラーが出てしまい、各種記事を検索して試していますがうまくいきません。
|
11
11
|

|
12
12
|
|
13
13
|
親である@lessonには子であるlesson_imagesが入っていることは、以下の通り確認しております。
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -13,25 +13,26 @@
|
|
13
13
|
親である@lessonには子であるlesson_imagesが入っていることは、以下の通り確認しております。
|
14
14
|
|
15
15
|
```ここに言語を入力
|
16
|
-
|
16
|
+
@lesson.lesson_images
|
17
|
+
LessonImage Load (0.6ms) SELECT "lesson_images".* FROM "lesson_images" WHERE "lesson_images"."lesson_id" = $1 [["lesson_id", 45]]
|
17
|
-
=> [#<LessonImage:
|
18
|
+
=> [#<LessonImage:0x007ff5b3f6a0a8
|
18
19
|
id: 7,
|
19
20
|
lesson_id: 45,
|
20
|
-
lesson_image:
|
21
|
+
lesson_image: "a.JPG",
|
21
22
|
created_at: Wed, 12 Apr 2017 22:58:18 JST +09:00,
|
22
|
-
updated_at:
|
23
|
+
updated_at: Thu, 13 Apr 2017 08:41:47 JST +09:00>,
|
23
|
-
#<LessonImage:
|
24
|
+
#<LessonImage:0x007ff5b3f69f68
|
24
25
|
id: 8,
|
25
26
|
lesson_id: 45,
|
26
|
-
lesson_image:
|
27
|
+
lesson_image: "f.jpg",
|
27
28
|
created_at: Wed, 12 Apr 2017 22:58:45 JST +09:00,
|
28
|
-
updated_at:
|
29
|
+
updated_at: Thu, 13 Apr 2017 08:41:47 JST +09:00>,
|
29
|
-
#<LessonImage:
|
30
|
+
#<LessonImage:0x007ff5b3f69e28
|
30
31
|
id: 9,
|
31
32
|
lesson_id: 45,
|
32
|
-
lesson_image:
|
33
|
+
lesson_image: "v.jpg",
|
33
34
|
created_at: Wed, 12 Apr 2017 23:03:33 JST +09:00,
|
34
|
-
updated_at:
|
35
|
+
updated_at: Thu, 13 Apr 2017 08:43:03 JST +09:00>]
|
35
36
|
```
|
36
37
|
|
37
38
|
```ruby
|
1
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Carrierwaveでネストして
|
1
|
+
image_tag : Carrierwaveでネストしてuploadした複数の画像データを表示できない
|
body
CHANGED
@@ -1,10 +1,39 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
状況
|
2
|
+
---
|
3
|
+
- `Lesson(model)`が`LessonImage(model、画像です)`との間に`has_many / blongs_to(コードは以下に記載)`の関連付けを持っています。
|
4
|
+
- 画像Uploadには、`Carrierwave`を利用し、新規にLessonを作成する際に、`accepts_nested_attributes_for`と`fields_for`を使って同時に子レコードである`LessonImage`も作成しています。
|
5
|
+
- リスト`Lesson`が`has_many`で所有する`LessonImage`の写真は0-3枚としています。画像をネストして複数アップロードすることはできています。
|
6
|
+
- 1つの`Lesson`に対して最大3つの'LessonImage'が付いており、それぞれは、例えば`@lesson.lesson_images[0]`や`@lesson.lesson_images.last`で呼べるのですが、以下記載の通りエラーが出てしまいます。 `form_for`と`fields_for`を利用していることも原因な気もしておりますが、解決策わからず、御指南頂けますと助かります。
|
3
7
|
|
8
|
+
出てしまうエラー
|
9
|
+
---
|
4
|
-
|
10
|
+
`undefined method `url' for #<LessonImage:0x007fdeb2b92c68>`といったエラーが出てしまい、各種記事を検索して試していますがうまくいきません。
|
11
|
+

|
5
12
|
|
6
|
-
|
13
|
+
親である@lessonには子であるlesson_imagesが入っていることは、以下の通り確認しております。
|
7
14
|
|
15
|
+
```ここに言語を入力
|
16
|
+
pry(main)> @lesson.lesson_images
|
17
|
+
=> [#<LessonImage:0x007ff5afe25e80
|
18
|
+
id: 7,
|
19
|
+
lesson_id: 45,
|
20
|
+
lesson_image: nil,
|
21
|
+
created_at: Wed, 12 Apr 2017 22:58:18 JST +09:00,
|
22
|
+
updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
|
23
|
+
#<LessonImage:0x007ff5afe25ae8
|
24
|
+
id: 8,
|
25
|
+
lesson_id: 45,
|
26
|
+
lesson_image: nil,
|
27
|
+
created_at: Wed, 12 Apr 2017 22:58:45 JST +09:00,
|
28
|
+
updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
|
29
|
+
#<LessonImage:0x007ff5afe25980
|
30
|
+
id: 9,
|
31
|
+
lesson_id: 45,
|
32
|
+
lesson_image: nil,
|
33
|
+
created_at: Wed, 12 Apr 2017 23:03:33 JST +09:00,
|
34
|
+
updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>]
|
35
|
+
```
|
36
|
+
|
8
37
|
```ruby
|
9
38
|
関連付け
|
10
39
|
#Lesson Model
|
@@ -13,6 +42,15 @@
|
|
13
42
|
|
14
43
|
#LessonImage Model
|
15
44
|
belongs_to :lesson, optional: true(#Rails 5.0.0.1を利用)
|
45
|
+
|
46
|
+
#LessonImageのテーブル
|
47
|
+
# Table name: lesson_images
|
48
|
+
#
|
49
|
+
# id :integer not null, primary key
|
50
|
+
# lesson_id :integer
|
51
|
+
# lesson_image :string
|
52
|
+
# created_at :datetime not null
|
53
|
+
# updated_at :datetime not null
|
16
54
|
```
|
17
55
|
```ruby
|
18
56
|
#edit.html.erb
|
@@ -20,24 +58,17 @@
|
|
20
58
|
<%= render "form", f: f %>
|
21
59
|
<%= f.submit "編集する" %>
|
22
60
|
|
23
|
-
#_form.html.erb
|
61
|
+
#_form.html.erb
|
24
62
|
<%= f.label :title, "レッスン名" %>
|
25
63
|
<%= f.text_field :title %>等、lessonモデルのcolumnが並びます。
|
26
|
-
|
64
|
+
|
27
|
-
<%= limg.label :lesson_image, "レッスン写真" %>
|
28
|
-
<%= limg.file_field :lesson_image, class:"form-control-file", id:"exampleInputFile" %>
|
29
|
-
<% end %>
|
30
|
-
```
|
31
|
-
動作させたい`_form.html.erb`は以下の通りですが、色々やってみるののの、うまく動きません。`field_for`以下に問題があることは認識しています。(当然ですが。。。)
|
32
|
-
```ruby
|
33
|
-
#_form.html.erb
|
34
65
|
<%= f.fields_for :lesson_images do |img|%>
|
35
|
-
<% if @lesson.lesson_images.exists? %>
|
36
|
-
|
66
|
+
<%= image_tag img.object.url %>
|
37
67
|
<%= img.check_box :remove_lesson_image %>画像を削除する<br>
|
38
|
-
<% end %>
|
39
|
-
|
40
68
|
<%= img.label :lesson_image, "レッスン写真" %>
|
41
69
|
<%= img.file_field :lesson_image %>
|
70
|
+
<%= img.label :lesson_image, "レッスン写真" %>
|
71
|
+
<%= img.file_field :lesson_image %>
|
42
72
|
<% end %>
|
73
|
+
|
43
74
|
```
|