1

我一直在寻找答案很长时间,但没有任何帮助。我一直在关注 Carrierwave 的 Github 页面上的教程(https://github.com/carrierwaveuploader/carrierwave/blob/master/README.md#multiple-file-uploads)。就像说明(更换餐厅的用户和照片的头像)我有以下内容(我使用的是 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

当我这样做时,我得到的是只有第一张照片保存在 restaurant.photo 中,但在 Cloudinary 上我收到了所有照片。

额外的事情是我不能使用 tag-as-tagable gem ,因为如果我这样做了,我会收到这个错误:

PG::UndefinedFunction: ERROR:  could not identify an equality operator for type json
LINE 1: ..., restaurants.created_at, restaurants.updated_at, restaurant...

这与将照片作为 json 列的餐厅表有关。也没有找到解决这个问题的方法。我希望我可以继续使用 Acts 作为可标记,因为它对于我正在构建的类型网站非常有用。所以现在我不得不评论与这颗宝石有关的一切。

有人可以帮我摆脱这个烂摊子吗?我会很感激的。

4

0 回答 0