0

This is an excercise case where images are stored as url using an Image polymorphic model, and passed to movies through Image

Models

# app/models/movie.rb

class Movie < ApplicationRecord
  has_one :image, as: :imageable
end
# app/models/image.rb

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true
end

movies controller

class MoviesController < ApplicationController
  def index
    @movies = Movie.all
  end

  def show
    @movie = Movie.find(params[:id])
  end
end

routes

Rails.application.routes.draw do
 resources :movies do
    member do
      get 'preview'
    end
  end
end

Movies Views

Index

<h1>Movies</h1>

<div id="movies">
  <% @movies.each do |movie| %>
    <%= render movie %>
    <p>
      <%= link_to "Show this movie", movie %>
    </p>
  <% end %>
</div>

_movie (partial) -> here is the problem

I can't use <%= image_tag movie.image.url %> instead I used <img src=<%= movie.image.url %>, class="card-img-top">

if not I get this error

image_tag Nil location provided. Can't build URI.
<div id="<%= dom_id movie %>" class="card h-100" style="width: 18rem;">
  <img src=<%= movie.image.url %>, class="card-img-top">

  <p>
    <strong>Name:</strong>
    <%= movie.title %>
  </p>
</div>

preview

and in this view I use image_tag without problems.

<h1> <%= @movie.title %> </h1>


<%= image_tag @movie.image.url %>

########################################

Any ideas of what's happening here?

I'm using esbuild and using the local server with the command ./bin/dev

Mauricio
  • 17
  • 5
  • Just from the error message it looks like not all movies has a corresponding image record and when that happens the image_tag would throw a Nil location error. I would start there and also refer to this post where they discuss how to manage the display when the location is Nil - https://stackoverflow.com/questions/53510040/carrierwave-argument-error-nil-location-provided-cant-build-uri-for-an-image?rq=2 – ck_arjun Jul 17 '23 at 17:28
  • Thats not the case, I'm sure that all movies have the image url, – Mauricio Jul 17 '23 at 22:30

0 Answers0