繁体   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