質問編集履歴

4

情報追加

2017/04/13 03:00

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -86,7 +86,13 @@
86
86
 
87
87
  #LessonImage Model
88
88
 
89
+ class LessonImage < ApplicationRecord
90
+
89
- belongs_to :lesson, optional: true(#Rails 5.0.0.1を利用)
91
+ belongs_to :lesson, optional: true(#Rails 5.0.0.1を利用)
92
+
93
+ mount_uploader :lesson_image, LessonImageUploader
94
+
95
+ end
90
96
 
91
97
 
92
98
 

3

修正

2017/04/13 03:00

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  ---
18
18
 
19
- `undefined method `url' for #<LessonImage:0x007fdeb2b92c68>`といったエラーが出てしまい、各種記事を検索して試していますがうまくいきません。
19
+ undefined method `url' for #<LessonImage:0x007fdeb2b92c68>`といったエラーが出てしまい、各種記事を検索して試していますがうまくいきません。
20
20
 
21
21
  ![イメージ説明](6dc8aaec6ad6d0ecc1b9ca57c502e23a.png)
22
22
 

2

修正

2017/04/12 23:59

投稿

innjera
innjera

スコア132

test CHANGED
File without changes
test CHANGED
@@ -28,43 +28,45 @@
28
28
 
29
29
  ```ここに言語を入力
30
30
 
31
- pry(main)> @lesson.lesson_images
31
+ @lesson.lesson_images
32
32
 
33
+ LessonImage Load (0.6ms) SELECT "lesson_images".* FROM "lesson_images" WHERE "lesson_images"."lesson_id" = $1 [["lesson_id", 45]]
34
+
33
- => [#<LessonImage:0x007ff5afe25e80
35
+ => [#<LessonImage:0x007ff5b3f6a0a8
34
36
 
35
37
  id: 7,
36
38
 
37
39
  lesson_id: 45,
38
40
 
39
- lesson_image: nil,
41
+ lesson_image: "a.JPG",
40
42
 
41
43
  created_at: Wed, 12 Apr 2017 22:58:18 JST +09:00,
42
44
 
43
- updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
45
+ updated_at: Thu, 13 Apr 2017 08:41:47 JST +09:00>,
44
46
 
45
- #<LessonImage:0x007ff5afe25ae8
47
+ #<LessonImage:0x007ff5b3f69f68
46
48
 
47
49
  id: 8,
48
50
 
49
51
  lesson_id: 45,
50
52
 
51
- lesson_image: nil,
53
+ lesson_image: "f.jpg",
52
54
 
53
55
  created_at: Wed, 12 Apr 2017 22:58:45 JST +09:00,
54
56
 
55
- updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
57
+ updated_at: Thu, 13 Apr 2017 08:41:47 JST +09:00>,
56
58
 
57
- #<LessonImage:0x007ff5afe25980
59
+ #<LessonImage:0x007ff5b3f69e28
58
60
 
59
61
  id: 9,
60
62
 
61
63
  lesson_id: 45,
62
64
 
63
- lesson_image: nil,
65
+ lesson_image: "v.jpg",
64
66
 
65
67
  created_at: Wed, 12 Apr 2017 23:03:33 JST +09:00,
66
68
 
67
- updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>]
69
+ updated_at: Thu, 13 Apr 2017 08:43:03 JST +09:00>]
68
70
 
69
71
  ```
70
72
 

1

修正

2017/04/12 23:44

投稿

innjera
innjera

スコア132

test CHANGED
@@ -1 +1 @@
1
- Carrierwaveでネストしてuploadeした複数の画像データをform_for/fields_forメソッド内の呼び出し方
1
+ image_tag : Carrierwaveでネストしてuploadした複数の画像データを表示きない
test CHANGED
@@ -1,14 +1,72 @@
1
- `Lesson(model)`が`LessonImage(model、画像です)`との間に以下の関連付けを持っています。
1
+ 状況
2
2
 
3
- 画像Uploadには、`Carrierwave`を利用し、新規にLessonを作成する際に、`accepts_nested_attributes_for`と`fields_for`を使って同時に子レコードである`LessonImage`も作成しています。`Lesson`が`has_many`で所有する`LessonImage`の写真は0-3枚としています。画像をネストして複数アップロードすることはできています。
3
+ ---
4
+
5
+ - `Lesson(model)`が`LessonImage(model、画像です)`との間に`has_many / blongs_to(コードは以下に記載)`の関連付けを持っています。
6
+
7
+ - 画像Uploadには、`Carrierwave`を利用し、新規にLessonを作成する際に、`accepts_nested_attributes_for`と`fields_for`を使って同時に子レコードである`LessonImage`も作成しています。
8
+
9
+ - リスト`Lesson`が`has_many`で所有する`LessonImage`の写真は0-3枚としています。画像をネストして複数アップロードすることはできています。
10
+
11
+ - 1つの`Lesson`に対して最大3つの'LessonImage'が付いており、それぞれは、例えば`@lesson.lesson_images[0]`や`@lesson.lesson_images.last`で呼べるのですが、以下記載の通りエラーが出てしまいます。 `form_for`と`fields_for`を利用していることも原因な気もしておりますが、解決策わからず、御指南頂けますと助かります。
4
12
 
5
13
 
6
14
 
15
+ 出てしまうエラー
16
+
17
+ ---
18
+
7
- 問題は、`Lesson`modelをedit(update)する際に、既にuploadしてある画像、(画像がある場合)、表示のですがうまく表示できません。
19
+ `undefined method `url' for #<LessonImage:0x007fdeb2b92c68>`といったエラーが出てまい、各種記事検索て試してすがうまくきません。
20
+
21
+ ![イメージ説明](6dc8aaec6ad6d0ecc1b9ca57c502e23a.png)
8
22
 
9
23
 
10
24
 
11
- 1つの`Lesson`対して最大3つの'LessonImage'が付いており、それぞれ、例えば`@lesson.lesson_images[0]`や`@lesson.lesson_images.last`で呼べのですが、以下記載の通り `form_for`と`fields_for`の中でどの様に記載たら良いのかわからず、御指南頂けますと助かります。
25
+ 親である@lessonには子であるlesson_imagesが入っていことは、以下の通り確認ております。
26
+
27
+
28
+
29
+ ```ここに言語を入力
30
+
31
+ pry(main)> @lesson.lesson_images
32
+
33
+ => [#<LessonImage:0x007ff5afe25e80
34
+
35
+ id: 7,
36
+
37
+ lesson_id: 45,
38
+
39
+ lesson_image: nil,
40
+
41
+ created_at: Wed, 12 Apr 2017 22:58:18 JST +09:00,
42
+
43
+ updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
44
+
45
+ #<LessonImage:0x007ff5afe25ae8
46
+
47
+ id: 8,
48
+
49
+ lesson_id: 45,
50
+
51
+ lesson_image: nil,
52
+
53
+ created_at: Wed, 12 Apr 2017 22:58:45 JST +09:00,
54
+
55
+ updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>,
56
+
57
+ #<LessonImage:0x007ff5afe25980
58
+
59
+ id: 9,
60
+
61
+ lesson_id: 45,
62
+
63
+ lesson_image: nil,
64
+
65
+ created_at: Wed, 12 Apr 2017 23:03:33 JST +09:00,
66
+
67
+ updated_at: Wed, 12 Apr 2017 23:03:33 JST +09:00>]
68
+
69
+ ```
12
70
 
13
71
 
14
72
 
@@ -28,6 +86,24 @@
28
86
 
29
87
  belongs_to :lesson, optional: true(#Rails 5.0.0.1を利用)
30
88
 
89
+
90
+
91
+ #LessonImageのテーブル
92
+
93
+ # Table name: lesson_images
94
+
95
+ #
96
+
97
+ # id :integer not null, primary key
98
+
99
+ # lesson_id :integer
100
+
101
+ # lesson_image :string
102
+
103
+ # created_at :datetime not null
104
+
105
+ # updated_at :datetime not null
106
+
31
107
  ```
32
108
 
33
109
  ```ruby
@@ -42,39 +118,23 @@
42
118
 
43
119
 
44
120
 
45
- #_form.html.erb ->登録してある画像を表示しない場合(問題なく動作します)
121
+ #_form.html.erb
46
122
 
47
123
  <%= f.label :title, "レッスン名" %>
48
124
 
49
125
  <%= f.text_field :title %>等、lessonモデルのcolumnが並びます。
50
126
 
51
- <%= f.fields_for :lesson_images do |limg|%>
52
127
 
53
- <%= limg.label :lesson_image, "レッスン写真" %>
54
-
55
- <%= limg.file_field :lesson_image, class:"form-control-file", id:"exampleInputFile" %>
56
-
57
- <% end %>
58
-
59
- ```
60
-
61
- 動作させたい`_form.html.erb`は以下の通りですが、色々やってみるののの、うまく動きません。`field_for`以下に問題があることは認識しています。(当然ですが。。。)
62
-
63
- ```ruby
64
-
65
- #_form.html.erb
66
128
 
67
129
  <%= f.fields_for :lesson_images do |img|%>
68
130
 
69
- <% if @lesson.lesson_images.exists? %>
70
-
71
- <%= image_tag @lesson.lesson_images.first.lesson_image.url %>
131
+ <%= image_tag img.object.url %>
72
132
 
73
133
  <%= img.check_box :remove_lesson_image %>画像を削除する<br>
74
134
 
75
- <% end %>
135
+ <%= img.label :lesson_image, "レッスン写真" %>
76
136
 
77
-
137
+ <%= img.file_field :lesson_image %>
78
138
 
79
139
  <%= img.label :lesson_image, "レッスン写真" %>
80
140
 
@@ -82,4 +142,6 @@
82
142
 
83
143
  <% end %>
84
144
 
145
+
146
+
85
147
  ```