Actors I step 8 actor_path(@actor) is not working


#1

my routes are:

get '/actors' => 'actors#index'
get '/actors/:id' => 'actors#show', as: :actor

my index is:

<div class="main actor-index">
      <div class="container">
        <div class="row">

          <!--
          To do: Loop through movies and display each one with this HTML-->
        <% @actors.each do |t| %>
          <div class="actor col-xs-2">
            <%= image_tag t.image %>
            <h3> <%= t.first_name %> <%= t.last_name %> </h3>
            <%= link_to "Learn more", actor_path(@actor) %>
            <p> <%= t.bio %> </p> 
          </div>
          <% end %>
          
      </div>
</div>

when I try http://localhost:8000/actors I get the error:

howing /home/ccuser/workspace/learn-rails-many-to-many/app/views/actors/index.html.erb where line #11 raised:

No route matches {:action=>"show", :controller=>"actors", :id=>nil} missing required keys: [:id]
Extracted source (around line #11):

8             <div class="actor col-xs-2">
9              <%= image_tag t.image %>
10               <h3> <%= t.first_name %> <%= t.last_name %> </h3>
11              <%= link_to "Learn more", actor_path(@actor) %>
12              <p> <%= t.bio %> </p> 
13            </div>
14           <% end %>

And I get the same exact issue with the movies path


#2

Hi @fahadfahad,

I think you need to define @actor. Could you please post your Actors controller code?


#3

her is my actor controller code

 class ActorsController < ApplicationController
 	def index
 		@actors = Actor.all
 	end
 	def show
 		@actor = Actor.find(params[:id])
 	        @movies = @actor.movies

 	end	
 end

#4

I think I got it BUT can you confirm:

the controller should have actors and not actor.

 class ActorsController < ApplicationController
  	def index
  		@actors = Actor.all
  	end
  	def show
  		@actors = Actor.find(params[:id])
  	        @movies = @actor.movies

  	end	
  end

#5

@fahadfahad No, the first controller code you have is correct. I'm not sure why it's not working, though :confused:

Could you re-run the code when it's like that? It shouldn't be throwing an error with that code.


#6

@zystvan I reverted back to the code in the controller and now. I click on http://localhost:8000/actors and I get the error I noted above with the path UNLESS the path looks like this <%= link_to "Learn more", actor_path(@actors) %> with the 's' on @actors.

I get to the actors page and now I will click on any actor's "Learn more" and I get this error:

https://localhost/actors/%23%3CActor::ActiveRecord_Relation:0x000000047f05b8%3E

Couldn't find Actor with 'id'=#

end
  def show
    @actor = Actor.find(params[:id])
    @movies = @actor.movies
    
  end

with the @actor = Actor.find(params[:id]) highlighted in red

And I get the same error when I click on a movie and try to see it's actors....I wonder if I messed up the many-to-many relationship!!!


#7

@fahadfahad

I get the error I noted above with the path UNLESS the path looks like this <%= link_to "Learn more", actor_path(@actors) %> with the 's' on @actors.

The problem with that is, @actors doesn't exist - you created the variable as a singular, @actor.

I wonder if I messed up the many-to-many relationship!

I can't think of anything else, so I expect that that is the problem :confused:


Could you try navigating to https://localhost:8000/actors/1 in the Codecademy virtual browser? (you may have to substitute 8000 for 8080)


#8

@zystvan So I'm a bit confused about that. what should the variable be at the controller? plural or singular ? and what should it be at the html.erb page _path part?


#9

@fahadfahad It should be singular for both of them. You can't use an undefined plural variable, when you've only defined the singular version. And, since @actor is only holding one actor in it (the current one, specified by the ID in the URL), you should use singular rather than plural for it.


You might want to take the Ruby course first, then come back to this.


#10

thanks @zystvan I have done 70% of the ruby course and nowhere it talks about paths or (params[:id]). I have a relatively OK experience with MVC coming from python/djnago I have no idea why can't I do

@actors = Actor.find(params[:id])

why does it have to be
@actor = Actor.find(params[:id])

You don't have to answer this btw I don't mean to confuse you I just don't get it. it is a variable and I can call it anything as long as I reference it and not minus one letter at the end.


#11

at this point it seems to be working except one thing and that being is when I click on a http://localhost:8000/actors I correctly see the list of actors with a link below each one to "learn more" BUT when I click learn more I get

Couldn't find Actor with 'id'=#
Extracted source (around line #6):
end
def show
@actor = Actor.find(params[:id])
@movies = @actor.movies

end

and the browser shows that it is trying to go to http://localhost:8000/actors/%23%3CActor::ActiveRecord_Relation:0x007f0027fe19a0%3E


#12

@fahadfahad

why can't I do

@actors = Actor.find(params[:id])

why does it have to be
@actor = Actor.find(params[:id])

There are two primary reasons:

  1. Codecademy's SCT is expecting you to name your variables the way they expect; in this case, @actor.
  2. The reasons we use words for our variable names is so we can understand what's in them - otherwise, why not just use 1, 2, 3, etc., for our variable names? - and @actors is plural, meaning that it contains more than one actor, which is not the case and will make debugging and understanding your code more difficult for everyone.

The reason your actor_path variable isn't working is because you're passing in a nonexistent variable, @actor, in actors/index.html.erb here:

<%= link_to "Learn more", actor_path(@actor) %>

You need to use the t variable as you loop through @actors to give the link, just like you're doing to show their name and biography.


#13

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