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