5.3 Movies||


#1

What is wrong?

link to my exercise: https://www.codecademy.com/courses/learn-rails/lessons/many-to-many/exercises/many-to-many-movies-ii?action=lesson_resume

and movies controller:


#2

Please post the raw code for your app/views/movies/index.html.erb and the config/routes.rb file. The above doesn't show any obvious issues.


#3


#4

Images are not raw code. How are we to test this? All a picture reveals is what we can see, not what we can easily test. The operative word being, 'easily'. Please post raw text code. Thank you.


#5

routes:

Rails.application.routes.draw do
  get "/movies" => "movies#index"
  get "/movies/:id", to: "movies#show", as: "movie"

view index:

  <% @movies.each do |movie| %>
     <%= image_tag movie.image %>
      <h3><%= movie.title %> </h3>
      <p><%= movie.release_year %> </p>
     <% end %>

movie controller:

class MoviesController < ApplicationController
  def index
    @movies = Movie.all
  end
  
  def show
    @movie = Movie.find(params[:id])
    @actors = @movie.actors
  end
end

view show:

<div class="main movie-show">
  <div class="container">
    <div class="movie">
      <!-- Display the movie's info here -->
      <div class="info">
      <%= image_tag @movie.image %>
        <!-- movie image -->
        <h3 class="movie-title"><%= @movie.title %> </h3>
        <p class="movie-release-year"> <%= @movie.release_year %> </p>
        <p class="movie-plot"> <%= @movie.plot %> </p>
      </div>
    </div>

    <h2>Cast</h2>
    <!-- Display each actor's info here -->
    <div class="actor">
      <%= image_tag @actor.image %>
      <!-- actor image -->
      <h3 class="actor-name"><%= @actor.first_name %> <%= @actor.last_name %> </h3>
      <p class="actor-bio"> <%= @actor.bio %> </p>
    </div>
  </div>
</div>

Sorry there are some problems to show the entire code of views. Is this OK? Thanks!


#6

So could you please check the code and help me with the bug? Thank you!


#7

should this be,

get "/movies/:id => "movies#show", as: :movie

??


#8

I changed the code and still the same question: "undefined 'image' method"


#9

You have the actors in the page, but not in the routes.db file. Could that be the cause of the problem?


#10

Maybe...how may I solve this? I mean, what code should I add to the routes?


#11

Create a route for /actors/ similar to movies. that points to the index and another that points to show. Use as: :actor


#12

Rails.application.routes.draw do
get "/movies" => "movies#index"
get "/movies/:id" => "movies#show", as: :movie
get "/actors" => "actors#index"
get "/actors/:id" => "actors#show", as: :actor

I write like this and still the "undefined method 'image'" problem...


#13

This brings us to the real issue... There should be a loop over the @actors.


#14

But there is no loop over the @movies either, should I add the loop to movies too?


#15

The movies/show page takes only one movie which id is in the URL, but there are multiple actors in the cast.

<% @actors.each do |actor| %>
    <!-- actor div -->
<% end %>

#16

Yes this worked...finally...:smiley:
Not easy...
I shall know that multiple factors need a loop to display
Thank you!


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.