0

This is my first question stackoverflow.

I tried to modify upload multiple files at once.

new.html.erb

<%= form_for(@attractions) do |f| %>
    <div class="form-group">
      <%= f.label :name, t("attraction.name"), class: "col-md-2 control-label" %>
      <div class="col-md-10">
        <%= f.text_field :name, class: "form-control" %>
      </div>
    </div>

    <div class="form-group">
      <%= f.fields_for :attraction_photos do |att_photo| %>
          <%= att_photo.label :photo, t("attraction.attraction_photo"), class: "col-md-2 control-label" %>
          <div class="col-md-10">
            <%= att_photo.file_field :photo, class: "form-control", multiple: true %> // <==== I add this
          </div>
      <% end %>
    </div>

attraction_controller

def new
  @attractions = Attraction.new
  @attractions.attraction_photos.build
end

def create
  @attractions = current_user.attractions.build(attraction_params)

  if @attractions.save
    flash[:success] = "success"
    redirect_to attractions_path
  else
    flash[:danger] = @attractions.errors.full_messages
    render action: :new
  end
end

def attraction_params
  params.require(:attraction).permit(:name, :longitude, :latitude, :address, :phone, :category_id, attraction_photos_attributes: [:photo => []])
end

I got the params is

{
    "name" => "name", 
    "longitude" => "111",
    "latitude" => "222", 
    "address" => "addresss", 
    "phone" => "phone", 
    "category_id" => "1", 
     "attraction_photos_attributes" => {
        "0" => {
            "photo" => [#, @original_filename = "aaaaaaaaaaaa.png", @content_type = "image/png", @headers = "Content-Disposition: form-data; name=\"attraction[attraction_photos_attributes][0][photo][]\"; filename=\"aaaaaaaaaaaa.png\"\r\nContent-Type: image/png\r\n" > , #, @original_filename = "bbbbbbbbbbb.png", @content_type = "image/png", @headers = "Content-Disposition: form-data; name=\"attraction[attraction_photos_attributes][0][photo][]\"; filename=\"bbbbbbbbbbb.png\"\r\nContent-Type: image/png\r\n" > ]
        }
    }
}

I think I should use a loop to create photos. But the data structure isn't I want. Thanks for your help.

Eidt

attraction model

class Attraction < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :attraction_photos, dependent: :destroy

  accepts_nested_attributes_for :attraction_photos

  ...
end

attraction photo model

class AttractionPhoto < ActiveRecord::Base
  belongs_to :attraction

  mount_uploader :photo, AttractionImageUploader
end
lighter
  • 2,808
  • 3
  • 40
  • 59

1 Answers1

0

you have two options

first option use a json field in your db to upload the pictures. Not all db allow this, for example some mysql2 db do not allow you to use the json type field in your db. Also using this is not the ruby on rails best practice

rails carrierwave, one user has many image

The other option is from this solution, you will not use a JSON field in the db, but create a separate table

Rails 4 multiple image or file upload using carrierwave

now please tell me why you do not want to use either this solutions, because I implemented both and I will try to help you

Also from your question it is not clear the issue you are having

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57