[RUBY] [Register multiple photos] Register multiple rails images at the same time Primitive power technique

【goal】

Register multiple images together at one time when saving data

【merit】

■ Improved understanding of association ■ Improvement of application functions ■ Improvement of UX

【Development environment】

■ Mac OS catalina ■ Ruby on Rails (5.2.4.2) ■ Virtual Box:6.1 ■ Vagrant: 2.2.7

【Implementation】

Application creation

mac.terminal


$ rails new photo

refile setting

app.gemfile


gem "refile", require: "refile/rails", github: 'manfe/refile'
gem "refile-mini_magick"

mac.terminal


$ bundle install

Modeling

mac.terminal


$ rails g scaffold Book name:string text:text
$ rails g model Image image_id:string book:references
$ rails db:migtrate

Edit model

models/book.rb


has_many :images, dependent: :destroy
accepts_attachment_for :images, attachment: :image, append: :true

models/image.rb


attachment :image
belongs_to book

Controller postscript

controllers/books_controller.rb


def book_params
      params.require(:book).permit(:name, :text, images_images: [])
end

View postscript

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>

* Add if refile key is requested

config/application.rb


Refile.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

If you have any corrections Please point out.

[I want to read it together]

■ Regarding refile setting https://qiita.com/salvage0707/items/2614c97a1f256027ef71

■ Regarding data type references https://qiita.com/ryouzi/items/2682e7e8a86fd2b1ae47

■ Change image send button https://qiita.com/tanaka-yu3/items/f4a0df867ca9f2476314

■ About form https://qiita.com/tanaka-yu3/items/50f54f5d4f4b8dfe19f3

Recommended Posts

[Register multiple photos] Register multiple rails images at the same time Primitive power technique
Implementation method of linking multiple images to one post and posting at the same time
Setting to start multiple units at the same time with Vagrant
FactoryBot Register multiple data with the same model
[Rails] Put the same restrictions on multiple pieces of information
[Rails] Register by attribute of the same model using Devise
FactoryBot Register multiple data with the same model
Setting to start multiple units at the same time with Vagrant
Prepare the format environment with "Rails" (VScode)
Run a DMN with the Camunda DMN Engine
[Rails] About Uglifier :: Error: Unexpected token: at the time of deployment
How to call multiple names at once in the same category