Multiple image uploads with Carrierwave and Rails 5

时间:2017-06-19 14:05:27

标签: mysql ruby-on-rails carrierwave

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.

1 个答案:

答案 0 :(得分:0)

而不是mount_uploader,使用mount_uploaders(末尾带有's')来指定CarrierWave他必须要有多个文件(doc here

然后当它完成后,他会给你一个包含每个文件路径的数组,所以你需要做的就是:

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

如果有必要,我让你调整代码,告诉我它是否有效