Ich bin neu bei Ruby on Rails. Ich erstelle eine App, die Fotos in der Galerie speichern kann. Ich habe versucht, ein Foto mit Büroklammer hochladen und es läuft erfolgreich. Aber jetzt möchte ich mehrere Fotos in derselben Galerie gleichzeitig mit Büroklammer hinzufügen. Ich verstehe nicht, wie ich es erreichen kann. Kann mir jemand helfen?Mehrere Bilder in Schienen hochladen
In gemfile habe ich "gem 'Büroklammer' geschrieben".
dies sind meine Modelle:
#Gallery Model
class Gallery < ActiveRecord::Base
has_many :gallery_photos
validates :name, presence: true
end
#GalleryPhoto Model
class GalleryPhoto < ActiveRecord::Base
belongs_to :gallery
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/gallery_photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/gallery_photos/:id/:style/:basename.:extension"
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png']
end
Das ist mein Controller für gallery_photo ist:
def create
@gallery_photo = GalleryPhoto.new(gallery_photo_params)
respond_to do |format|
if @gallery_photo.save
format.html { redirect_to galleries_galleryhome_path(id: @gallery_photo.gallery_id), notice: 'Gallery photo was successfully created.' }
format.json { render :show, status: :created, location: @gallery_photo }
else
format.html { render :new }
format.json { render json: @gallery_photo.errors, status: :unprocessable_entity }
end
end
end
private
def gallery_photo_params
params.require(:gallery_photo).permit(:gallery_id,:photo)
end
Dies ist meine Migrationsdatei:
class AddAttachmentPhotoToGalleryPhotos < ActiveRecord::Migration
def self.up
change_table :gallery_photos do |t|
t.attachment :photo
end
end
def self.down
remove_attachment :gallery_photos, :photo
end
end
Meiner Ansicht i die geschrieben haben, folgende Zeile zum Anhängen von Bildern:
<%= form_for @gallery_photo,html: { multipart: true},url: gallery_photos_path,method: :post do |f| %>
<div>
<%= f.file_field :photo, multiple: true %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Dies sind die Felder der Galerie Tabelle:
class CreateGalleryPhotos < ActiveRecord::Migration
def change
create_table :gallery_photos do |t|
t.references :gallery, index: true, foreign_key: true
t.timestamps null: false
end
end
end
und Anbaufelder werden durch die Migrationsdatei hinzugefügt, die ich oben erwähnt habe.
Was ist der Fehler werfen? – Sukanta
Es speichert die Attribute der Bilder nicht. – Vishal
können Sie die Tabellenfelder setzen? – Sukanta