Enregistrez plusieurs images ensemble à la fois lors de la sauvegarde des données
■ Meilleure compréhension de l'association ■ Amélioration des fonctions applicatives ■ Amélioration de l'UX
■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7
mac.terminal
$ rails new photo
app.gemfile
gem "refile", require: "refile/rails", github: 'manfe/refile'
gem "refile-mini_magick"
mac.terminal
$ bundle install
mac.terminal
$ rails g scaffold Book name:string text:text
$ rails g model Image image_id:string book:references
$ rails db:migtrate
models/book.rb
has_many :images, dependent: :destroy
accepts_attachment_for :images, attachment: :image, append: :true
models/image.rb
attachment :image
belongs_to book
controllers/books_controller.rb
def book_params
params.require(:book).permit(:name, :text, images_images: [])
end
views/books/_form.html.erb
<div class="field">
<%= form.label :photo %>
<%= form.attachment_field :photos_images, multiple: true, direct: true, presigned: true %>
</div>
views/books/show.html.erb
<div>
<% @book.images.each do |image| %>
<%= attachment_image_tag image, :image ,:fill, 200, 200 %>
<% end %>
</div>
config/application.rb
Refile.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Si vous avez des corrections Veuillez préciser.
■ Concernant le paramétrage du refile https://qiita.com/salvage0707/items/2614c97a1f256027ef71
■ Concernant les références de type de données https://qiita.com/ryouzi/items/2682e7e8a86fd2b1ae47
■ Changer le bouton d'envoi d'image https://qiita.com/tanaka-yu3/items/f4a0df867ca9f2476314
■ À propos du formulaire https://qiita.com/tanaka-yu3/items/50f54f5d4f4b8dfe19f3
Recommended Posts