5.3 Movies||


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:


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.



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.



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
  def show
    @movie = Movie.find(params[:id])
    @actors = @movie.actors

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>

    <!-- 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>

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


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


should this be,

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



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


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


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


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


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...


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


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


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 %>


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


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