How can I upload multiple images from a file selection window using Rails 4 and CarrierWave? I have a post_controller
and post_attachments
model. How can I do this?
Can someone provide an example? Is there a simple approach to this?
How can I upload multiple images from a file selection window using Rails 4 and CarrierWave? I have a post_controller
and post_attachments
model. How can I do this?
Can someone provide an example? Is there a simple approach to this?
If we take a look at CarrierWave's documentation, this is actually very easy now.
https://github.com/carrierwaveuploader/carrierwave/blob/master/README.md#multiple-file-uploads
I will use Product as the model I want to add the pictures, as an example.
Get the master branch Carrierwave and add it to your Gemfile:
Create a column in the intended model to host an array of images:
Run the migration
Add pictures to model Product
Add pictures to strong params in ProductsController
Allow your form to accept multiple pictures
In your views, you can reference the images parsing the pictures array:
If you choose several images from a folder, the order will be the exact order you are taking them from top to bottom.
When using the association
@post.post_attachments
you do not need to set thepost_id
.Also I figured out how to update the multiple file upload and I also refactored it a bit. This code is mine but you get the drift.
Here is my second refactor into the model:
Controller:
In motherboard model:
To do just follow these steps.
In gem file
Create post scaffold
Create post_attachment scaffold
In post.rb
In post_attachment.rb
In post_controller.rb
In views/posts/_form.html.erb
To edit an attachment and list of attachment for any post. In views/posts/show.html.erb
Update form to edit an attachment views/post_attachments/_form.html.erb
Modify update method in post_attachment_controller.rb
In rails 3 no need to define strong parameters and as you can define attribute_accessible in both the model and accept_nested_attribute to post model because attribute accessible is deprecated in rails 4.
For edit an attachment we cant modify all the attachments at a time. so we will replace attachment one by one, or you can modify as per your rule, Here I just show you how to update any attachment.
Some minor additions to the SSR answer:
accepts_nested_attributes_for does not require you to change the parent object's controller. So if to correct
to
then all these controller changes like these become redundant:
Also you should add
PostAttachment.new
to the parent object form:In views/posts/_form.html.erb
This would make redundant this change in the parent's controller:
For more info see Rails fields_for form not showing up, nested form
If you use Rails 5, then change
Rails.application.config.active_record.belongs_to_required_by_default
value fromtrue
tofalse
(in config/initializers/new_framework_defaults.rb) due to a bug inside accepts_nested_attributes_for (otherwise accepts_nested_attributes_for won't generally work under Rails 5).EDIT 1:
To add about destroy:
In models/post.rb
In views/posts/_form.html.erb
This way you simply do not need to have a child object's controller at all! I mean no any
PostAttachmentsController
is needed anymore. As for parent object's controller (PostController
), you also almost don't change it - the only thing you change in there is the list of the whitelisted params (to include the child object-related params) like this:That's why the
accepts_nested_attributes_for
is so amazing.