Register multiple images together at one time when saving data
■ Improved understanding of association ■ Improvement of application functions ■ Improvement of 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'
If you have any corrections Please point out.
■ 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