2017-10-25 1 views
0

Dies ist meine erste Frage stackoverflow.Schienen carrierwave, mehrere Dateien hochladen

Ich habe versucht, mehrere Dateien auf einmal zu ändern.

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 

ich die params bekam ist

{ 
    "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" > ] 
     } 
    } 
} 

Ich denke, ich sollte eine Schleife verwenden, um Fotos zu erstellen . Aber die Datenstruktur will ich nicht. Danke für Ihre Hilfe.

Eidt

Attraktion Modell

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

    accepts_nested_attributes_for :attraction_photos 

    ... 
end 

Attraktion Fotomodell

class AttractionPhoto < ActiveRecord::Base 
    belongs_to :attraction 

    mount_uploader :photo, AttractionImageUploader 
end 
+0

Bitte teilen Sie Ihren Modellcode. – Vishal

+0

@Vishal ich aktualisiert – lighter

+0

Haben Sie [dieses] referenziert (https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads)? – Vishal

Antwort

0

Sie haben zwei Möglichkeiten

erste Option Verwenden Sie ein json Feld in Ihrer Datenbank, um die Bilder hochzuladen. Nicht alle db erlauben dies, zum Beispiel einige mysql2 db erlauben es nicht, das json Feld in Ihrer Datenbank zu verwenden. Auch mit diesem auf Schienen beste Praxis nicht der Rubin ist

rails carrierwave, one user has many image

Die andere Option aus dieser Lösung ist, werden Sie nicht ein JSON-Feld in der db verwenden, sondern erstellen Sie eine separate Tabelle

Rails 4 multiple image or file upload using carrierwave

jetzt bitte sagen Sie mir, warum Sie entweder diese Lösungen nicht verwenden wollen, weil ich beide implementiert und ich werde versuchen, Sie aus Ihrer Frage

auch zu helfen, ist es das ist nicht klar, verklagen Sie haben

+0

Ich will nur üben. Und ich bin nicht vertraut mit DB speichern Bild, und ich möchte nur die Bilddatei hochladen. Das ist alles. – lighter

+0

@lighter vielen Dank lassen Sie es mich wissen, wenn Sie Hilfe benötigen –