简体   繁体   English

使用Carrierwave和Rails 5上传多个图像

[英]Multiple image uploads with Carrierwave and Rails 5

In my application I would like to see several photos. 在我的应用程序中,我想看几张照片。 I don't have a specific error, but I see only the name of the photo. 我没有特定的错误,但仅看到照片的名称。

In my controller: 在我的控制器中:

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

In my model: 在我的模型中:

mount_uploader :photos, PhotoUploader

In my form: 以我的形式:

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

In show view: 在显示视图中:

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

I am using a MySQL database. 我正在使用MySQL数据库。

Instead of mount_uploader , use mount_uploaders (with an 's' at the end) to specify to CarrierWave that he'll have to expect several files ( doc here ) 代替mount_uploader ,使用mount_uploaders (在末尾带有“ s”)向CarrierWave指定他将必须期待多个文件( 在此处查看文档

Then when it's done, he'll give back an array with each of your files's path, so all you need to do is the following: 然后,完成后,他将为您提供每个文件路径的数组,因此您所需要做的就是以下操作:

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

I let you adapt the code if necessary, just tell me if it works 我让您根据需要修改代码,只是告诉我是否可行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM