複数の画像ファイルを選択した後、それらをドラッグ&ドロップで並び替えて、順番を反映させてデータベースに保存したいです。
画像ファイルは、productsモデルのproducts_image1〜products_image30カラムに保存したいと考えています。
###環境
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin17]
Rails 4.2.6
mysql Ver 14.14 Distrib 5.5.56, for Linux (x86_64) using readline 5.1
###実装
以下のようなところで実装が止まっています。フォームでは画像ファイルの他に、エリア、名前、住所、ZIPファイルを送信できます。画像ファイル以外はデータベースへの保存に問題ありません。
html
1# form.html.erb 2 3<%= form_tag({ controller: '/manage/products', action: 'create' }, { method: 'post', multipart: true } ) do %> 4 <div class="input manage-product-create-form"> 5 <p class="row"><label class="label">エリア</label><%= select_tag :area_id, options_for_select(@areas.map{ |t| [t.name, t.id] }), include_blank: true %></p> 6 <p class="row"><label class="label">物件名</label><%= text_field_tag :building_name %></p> 7 <p class="row"><label class="label">住所</label><%= text_field_tag :address %></p> 8 <p class="row" id="js-selectFile"> 9 <label class="label">ZIP</label> 10 <%= file_field_tag :zip, id: "js-upload" %> 11 <%= hidden_field_tag :zip_cache, id: "js-upload-cache" %> 12 <button class="original-btn">ZIPファイルを選択</button> 13 <span class="icon">未選択</span> 14 </p> 15 16 #画像ファイルの並び替え 17 18 <div class="sort-box-container sortable"> 19 <% (1..30).each do |num| %> 20 <div class="sort-box"> 21 <%= file_field_tag :"products_image#{num}", class: "uploadFile" %> 22 <input type="text" class="form-control" readonly="" /> 23 <input type="hidden" id="result" name="result" /> 24 <div class="imagePreview item"> 25 </div> 26 </div> 27 <% end %> 28 </div> 29 </div> 30 <div class="actions"> 31 <%= button_tag type: 'submit', id: "submit", class: "btn-edit" do %> 32 <i class="fa fa-pencil" aria-hidden="true"></i> 作成 33 <% end %> 34 <%= link_to manage_products_path, class: "btn-back set-ml-10" do %> 35 <i class="fa fa-arrow-circle-left" aria-hidden="true"></i> 一覧に戻る 36 <% end %> 37 </div> 38<% end %>
jquery
1$(function() { 2 $(".sortable").sortable(); 3 $(".sortable").disableSelection(); 4 $("#submit").click(function() { 5 var result = $(".sortable").sortable("toArray"); 6 $("#result").val(result); 7 $("form").submit(); 8 }); 9 });
ruby
1# products_controller.rb 2 3def create 4 @product = Product.new( 5 area_id: params[:area_id], 6 building_name: params[:building_name], 7 address: params[:address], 8 zip: params[:zip] 9 # ここにproducts_image1〜products_image30カラムにparamsを入れる処理を書くと保存はされますが、ドラッグ&ドロップによる画像の順番が反映されません 10 )
###実現したいこと
たとえば画像イメージをドラッグ&ドロップで入れ替えた後にsubmitして、一番左にあるものがproducts_image1カラムに入り、その右にあるものがproducts_image2カラムに、最後のものがproducts_image30カラムに入るようにしたいです。
ご教示いただけることがあれば、何卒よろしくお願いいたします。
あなたの回答
tips
プレビュー