質問編集履歴

2

訂正

2017/02/19 05:45

投稿

besuko
besuko

スコア16

test CHANGED
File without changes
test CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
 
4
4
 
5
-
6
-
7
5
  ファイルのアップロードはgem'Dropzonejs'を使っています。
8
6
 
9
7
 
@@ -50,6 +48,50 @@
50
48
 
51
49
 
52
50
 
51
+
52
+
53
+ ###__《※2/19更新しました》__
54
+
55
+ turbgraphics200様のご助言頂きました[WIKI](https://github.com/enyo/dropzone/wiki/Combine-normal-form-with-Dropzone)と、[こちら](http://stackoverflow.com/questions/42225406/unable-to-combine-a-normal-form-with-dropzone-js)を参考にform_forへの組み込みは出来ました。
56
+
57
+ imageモデルへのshop_idを渡すところまで出来ましたが、肝心の「file(データ)」だけ渡されずにいます。
58
+
59
+ 出来る限りの事は試してみましたが、解決出来ずにおります。。
60
+
61
+ 誤った箇所を教えて頂けませんでしょうか。
62
+
63
+ なお、保存用ストレージはcloudinayを使っています。
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+ ```console
72
+
73
+ (0.1ms) begin transaction
74
+
75
+ SQL (0.8ms) INSERT INTO "images" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-02-19 04:51:31 UTC], ["updated_at", 2017-02-19 04:51:31 UTC]]
76
+
77
+ (1.8ms) commit transaction
78
+
79
+ (0.2ms) begin transaction
80
+
81
+ SQL (0.6ms) INSERT INTO "shops" ("user_id", "name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?) [["user_id", 2], ["name", "テスト"], ["address", "東京都港区赤坂1-1-1"], ["created_at", 2017-02-19 04:51:31 UTC], ["updated_at", 2017-02-19 04:51:31 UTC]]
82
+
83
+ SQL (0.2ms) INSERT INTO "images" ("shop_id", "created_at", "updated_at") VALUES (?, ?, ?) [["shop_id", 43], ["created_at", 2017-02-19 04:51:31 UTC], ["updated_at", 2017-02-19 04:51:31 UTC]]
84
+
85
+ SQL (0.1ms) UPDATE "images" SET "shop_id" = ?, "updated_at" = ? WHERE "images"."id" = ? [["shop_id", 43], ["updated_at", 2017-02-19 04:51:31 UTC], ["id", 41]]
86
+
87
+ (2.2ms) commit transaction
88
+
89
+ ```
90
+
91
+
92
+
93
+
94
+
53
95
  ###環境
54
96
 
55
97
  Ruby2.3.3
@@ -60,6 +102,8 @@
60
102
 
61
103
  ###Gem
62
104
 
105
+ gem 'cloudinary'
106
+
63
107
  gem 'dropzonejs-rails'
64
108
 
65
109
  ###モデル
@@ -134,6 +178,8 @@
134
178
 
135
179
  belongs_to :shop, optional: true
136
180
 
181
+
182
+
137
183
  mount_uploader :file, ImageUploader
138
184
 
139
185
  end
@@ -146,6 +192,12 @@
146
192
 
147
193
  ```ruby
148
194
 
195
+ class ShopsController < ApplicationController
196
+
197
+ before_action :set_shop, only: [:show, :edit, :update, :destroy]
198
+
199
+
200
+
149
201
  def new
150
202
 
151
203
  @shop = Shop.new
@@ -160,14 +212,16 @@
160
212
 
161
213
  @shop = current_user.shops.build(shop_params)
162
214
 
215
+ image = @shop.images.build
216
+
217
+ image.save!
218
+
163
219
  respond_to do |format|
164
220
 
165
221
  if @shop.save
166
222
 
167
223
  format.html { redirect_to @shop, notice: '登録しました' }
168
224
 
169
- #format.html { redirect_to "/shops/#{@shop.id}/images/new", notice: '画像を登録してください。' }
170
-
171
225
  format.json { render :show, status: :created, location: @shop }
172
226
 
173
227
  else
@@ -180,36 +234,40 @@
180
234
 
181
235
  end
182
236
 
237
+
238
+
239
+ private
240
+
241
+ def set_shop
242
+
243
+ @shop = Shop.find(params[:id])
244
+
183
- end
245
+ end
246
+
247
+
248
+
184
-
249
+ def shop_params
250
+
251
+ params.require(:shop).permit(:name, :address,
252
+
253
+ images_attributes: [:id, :file, :_destroy]
254
+
255
+ )
256
+
257
+ end
258
+
259
+ end
260
+
185
- ```
261
+ ```
186
-
187
-
188
-
262
+
263
+
264
+
265
+
266
+
189
- image_controller.rb
267
+ image_uploader.rb
190
268
 
191
269
  ```ruby
192
270
 
193
- def upload
194
-
195
- @shop = Shop.find(params[:shop_id])
196
-
197
- image = @shop.images.build(file: params['file'])
198
-
199
- image.save!
200
-
201
- render status: 200, json: @shop.images
202
-
203
- end
204
-
205
- ```
206
-
207
-
208
-
209
- image_uploader.rb
210
-
211
- ```ruby
212
-
213
271
  class ImageUploader < CarrierWave::Uploader::Base
214
272
 
215
273
  include Cloudinary::CarrierWave
@@ -310,20 +368,28 @@
310
368
 
311
369
  ```
312
370
 
371
+ <%= simple_form_for(@shop, :authenticity_token => true, html: { multipart: true, class: 'dropzone', id: 'new_post_form'}) do |f| %>
372
+
373
+
374
+
313
- <%= form_for (@shop) do |f| %>
375
+ <div class="form-group">
376
+
377
+ <%= f.label :images, :class => "col-sm-3 control-label" %>
378
+
379
+ <div class="dropzone-previews"></div>
314
380
 
315
381
  <div class="fallback">
316
382
 
317
- <%#= form_tag(upload_shop_images_path(@shop),
383
+ <%= f.fields_for :images do |image| %>
318
-
319
- :id => 'my-dropzone', :class => 'dropzone', method: :post) %>
384
+
320
-
321
- <!--※ ↓無理やりform_for形式にしてみましたが、やはり動きませんでした。-->
385
+ <%= image.input :file, :label => false %>
322
-
386
+
323
- <%= f.file_field :image, :id => 'my-dropzone', :class => 'dropzone', method: :post %>
387
+ <% end %>
324
388
 
325
389
  </div>
326
390
 
391
+ </div>
392
+
327
393
 
328
394
 
329
395
  <div class="form-group">
@@ -360,35 +426,41 @@
360
426
 
361
427
  <script type="text/javascript">
362
428
 
363
- Dropzone.options.myDropzone = {
429
+ Dropzone.options.newPostForm = {
364
-
430
+
365
- autoProcessQueue: false,
431
+ autoProcessQueue: false,
432
+
433
+ uploadMultiple: true,
434
+
435
+ parallelUploads: 100,
436
+
437
+ maxFiles: 100,
438
+
439
+ paramName: 'images[file]',
440
+
441
+ params:'images[shop_id]',
366
442
 
367
443
 
368
444
 
369
445
  init: function() {
370
446
 
371
- var submitButton = document.querySelector("#submit-all")
372
-
373
- myDropzone = this;
447
+ var myDropzone = this;
374
-
375
-
376
-
448
+
449
+
450
+
377
- submitButton.addEventListener("click", function() {
451
+ $("#submit-all").click(function (e) {
452
+
378
-
453
+ e.preventDefault();
454
+
455
+ e.stopPropagation();
456
+
379
- myDropzone.processQueue();
457
+ myDropzone.processQueue();
380
-
458
+
381
- });
459
+ });
382
-
383
-
384
-
385
- this.on("addedfile", function() {
386
-
387
- });
388
460
 
389
461
  }
390
462
 
391
- };
463
+ }
392
464
 
393
465
  </script>
394
466
 

1

修正

2017/02/19 05:45

投稿

besuko
besuko

スコア16

test CHANGED
File without changes
test CHANGED
@@ -146,6 +146,16 @@
146
146
 
147
147
  ```ruby
148
148
 
149
+ def new
150
+
151
+ @shop = Shop.new
152
+
153
+ @shop.images.build(file: params['file'])
154
+
155
+ end
156
+
157
+
158
+
149
159
  def create
150
160
 
151
161
  @shop = current_user.shops.build(shop_params)