I
I
IvanN7772015-05-16 20:59:17
Ruby on Rails
IvanN777, 2015-05-16 20:59:17

How to save file_field_tag ​​later in carrierwave?

Here is my form, the idea was to upload several files at once in one form, loop through and create several pictures for the gallery.
I store pictures in carrierwave, but how can I transfer the result to
it from a regular file field. The parameter returns only the file names. In theory, according to it, I should load it from the temporary storage tmp. But I don't know how to do it.
How to transfer the file value from the file_field_tag ​​field to @object.avatar. Or how can I organize another download of several files at once?

<%= bootstrap_form_for @gallery, :enctype =>"multipart/form-data" do |f| %>
        <div class="row">
          <div class="col-md-offset-4 col-md-4 col-xs-12">
            <%= f.hidden_field :tour_id %>
            <%= f.hidden_field :category_id %>
            <%= f.hidden_field :type %>
            <%= file_field_tag 'image_gallery1', :multiple => true%>
           <%= file_field_tag 'image_gallery2', :multiple => true%>
            <%= file_field_tag 'image_gallery2', :multiple => true%>


            <%= f.primary "Сохранить", class: "btn btn-primary" %>
          </div>
        </div>
    <% end %>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2015-05-16
@MAXOPKA

There must be an ActiveRecord model for gallery files. For example:

# Галерея
class Galery < ActiveRecord::Base
  has_many :photos
  accepts_nested_attributes_for :photos
end
#фотография
class Photo < ActiveRecord::Base
  belongs_to :galery
  # монтирование загрузчика :file -имя поля модели, где будет храниться имя файла
  mount_uploader :file, FileUploader
end
# загрузчик
class FileUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

HTML
<%= form_for @galery do |galery_form| %>
  <!-- такие то поля для галереи --->
  <%= galery_form.fields_for :photos, @photos do |photo_fields| %>
    Изображение: <%= photo_fields.file_field :file %>
  <% end %>
<% end %>

@photos - collection of created models in new action

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question