user273072545345
user273072545345

Reputation: 1566

undefined method `image' for Album

I am currently trying to display photos in an album.

This is the error that I receive (image below): enter image description here

These are the current specs so far:

Album Model

class Album < ApplicationRecord
    has_many :photos, dependent: :destroy
    validates :title, presence: true
    validates :description, presence: true
end

Photo Model

class Photo < ApplicationRecord
  belongs_to :album
  validates :name, presence: true
  validates :image, attachment_presence: true
  
  has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

Photo Controller

class PhotosController < ApplicationController

    def create
        @album = Album.find(params[:album_id])
        @photo = @album.photos.create(photo_params)
        redirect_to album_path(@album)
    end

    def destroy
        @album = Album.find(params[:album_id])
        @photo = @album.photos(params[:id])
        @photo.destroy
        redirect_to album_path(@album)
    end

    private 

        def photo_params
            params.require(:photo).permit(:name,:image)
        end
end

View Folder - Photos/_photos.html.erb - where the error is generated from

<p>
    <strong>Name:</strong>
    <%= photo.name %>
</p>
<p>
    <%= image_tag @album.image.url(:medium) %>
</p>

How do I fix this error?

Please let me know if more information is needed.

Thank you.

Upvotes: 0

Views: 129

Answers (1)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

Instead of this line

<%= image_tag @album.image.url(:medium) %>

try this

 <%= image_tag photo.image.url(:medium) %>

Upvotes: 2

Related Questions