简体   繁体   中英

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.

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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