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