簡體   English   中英

使用Carrierwave和Rails 5上傳多個圖像

[英]Multiple image uploads with Carrierwave and Rails 5

在我的應用程序中,我想看幾張照片。 我沒有特定的錯誤,但僅看到照片的名稱。

在我的控制器中:

private

# Use callbacks to share common setup or constraints between actions.
def set_place
  @place = Place.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def place_params
  params.require(:place).permit(:title, :description,
                                :price,:country,{photos: []},
end

在我的模型中:

mount_uploader :photos, PhotoUploader

以我的形式:

<%= f.file_field :photos, as: :file, multiple: true %>

在顯示視圖中:

<dd><%= image_tag @place.photos_url   %></dd>

我正在使用MySQL數據庫。

代替mount_uploader ,使用mount_uploaders (在末尾帶有“ s”)向CarrierWave指定他將必須期待多個文件( 在此處查看文檔

然后,完成后,他將為您提供每個文件路徑的數組,因此您所需要做的就是以下操作:

<dd>
  <% @place.photos.each do |p| %>
    <%= image_tag p %>
    <!-- if this doesn't work, try this -->
    <%= image_tag p.url %>
  <% end %>
</dd>

我讓您根據需要修改代碼,只是告訴我是否可行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM