I've been looking for a long time for the answer, but nothing has helped yet. I've been following the tutorial on the Github page for Carrierwave (https://github.com/carrierwaveuploader/carrierwave/blob/master/README.md#multiple-file-uploads). Just like the instructions (replacing user for restaurant and avatar for photo) I have the following (I'm using Rails 5.2.1):
rails g migration add_photos_to_restaurant photos:json
# app/models/restaurant.rb
class Restaurant < ApplicationRecord
belongs_to :user
mount_uploaders :photos, PhotoUploader
end
# app/views/restaurants/_form.html.erb
<div class="container" style="margin-bottom: 200px;">
<div class="row">
<div class="col-xs-6 col-sm-offset-3">
<div class="form-inputs">
<%= simple_form_for(@restaurant) do |f| %>
<% if @restaurant.errors.any? %>
<ul>
<% @restaurant.errors.full_messages.each do |message| %>
<li>
<%= message %>
</li>
<% end %>
</ul>
<% end %>
#[...]
<%= f.file_field :photos, multiple: true %>
</div>
<div class="form-actions">
<%= f.submit class: "btn btn-success" %>
<%= link_to 'Back', restaurants_path, class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
</div>
# app/controllers/restaurants_controller.rb
class RestaurantsController < ApplicationController
skip_before_action :authenticate_user!, only: [ :index, :show ]
before_action :set_restaurant, only: [ :show, :edit, :update, :destroy ]
def index
@restaurants = Restaurant.all
end
def show
end
def new
@restaurant = Restaurant.new
end
def create
@restaurant = Restaurant.new(restaurant_params)
@restaurant.user = current_user
if @restaurant.save
redirect_to restaurant_path(@restaurant)
else
render :new
end
end
def edit
end
def update
if @restaurant.update(restaurant_params)
redirect_to restaurant_path(@restaurant)
else
render :edit
end
end
def destroy
@restaurant.destroy
redirect_to restaurants_path
end
private
def set_restaurant
@restaurant = Restaurant.find(params[:id])
end
def restaurant_params
params.require(:restaurant).permit(
#[...],
{photos: []}
)
end
end
What I get when I do this is that only photo the first is saved inside restaurant.photo but on Cloudinary I receive all the photos.
An extra thing is that I can't use tag-as-tagable gem with this, because If I do I get this error:
PG::UndefinedFunction: ERROR: could not identify an equality operator for type json
LINE 1: ..., restaurants.created_at, restaurants.updated_at, restaurant...
This has something to do with the restaurants table having a photos as a json column. Haven't found a way around this either. I'm wishing that I can keep using Acts as taggable because it's very useful for the type website I'm building. So for now I had to comment everything out having to do with this gem.
Can someone please help me out of this mess? I'd appreciate it a lot.