vendredi 13 septembre 2019

ActiveStorage issues not uploading or not loading

So im trying to add the ability for users (devise) to upload images to a listing.

First off: I have a listings controller and the listings params I have it defined

def listing_params
  params.require(:listing).permit(:title, :description, images: [])
end

Next: In the listing model I have:

class Listing < ApplicationRecord
  belongs_to :user
  has_many_attached :images
end

And finally, I have the form and the index set up with

= simple_form_for @listing do |f|
  = f.input :title
  = f.file_field :images, multiple: true
  = f.text_area :description
  = f.button :submit

.listings.has-gutters
  - @listings.each do |listing|
    .box
      - @listings.images.each do |image|
        = image_tag(@listings.images[image])
      .listing-info
        %h3= link_to listing.title, listing_path(listing)
        %p= listing.description
        %p= listing.user.firstname

With all this, I would assume it would work but when uploading the images it gives me the error:

NoMethodError in Listings#index
Showing /Users/timbogdanov/Desktop/vend/app/views/listings/index.html.haml where line #5 raised:

undefined method `images' for #<Listing::ActiveRecord_Relation:0x00007f8ef1208578>
Extracted source (around line #5):

  - @listings.each do |listing|
    .box
      - @listings.images.each do |image|
        = image_tag(@listings.images[image])
      .listing-info
        %h3= link_to listing.title, listing_path(listing)

Aucun commentaire:

Enregistrer un commentaire