Great job! The app displays all actors that belong to a movie. The has_many :through
association lets us easily query for all actors that belong to a movie.
Instructions
Let’s add the ability to see all movies an actor has appeared in.
Generate a controller named Actors.
In the routes file, add a route that maps the URL /actors
to the Actors controller’s index
action.
Then in the controller, add the index
action to display a list of all actors.
In app/views/actors/index.html.erb, iterate through each actor and display the image, first name, and last name.
Back in the routes file, add another route to send requests to URLs like /actors/1
to the Actors controller’s show
action. Use as:
to name this route “actor”.
Then in the controller, add the show
action to display a specific actor and the filmography. To do this, first find an actor by id. Then retrieve all movies that belong to the actor.
In app/views/actors/show.html.erb:
- Display the actor’s image, first name, last name, and bio.
- Then iterate through each movie and display its title, image, and release year.
Finally in app/views/actors/index.html.erb below an actor’s name, use link_to
to add a link to that actor:
- Use “Learn more” for the link text
- By giving the
show
route a name of “actor”, Rails automatically creates a helper method namedactor_path
, so use it to generate a URL to a specific actor’s path
Visit http://localhost:8000/movies
in the browser. Click on a movie to see its cast.
Then visit http://localhost:8000/actors
. Click on an actor to see what movies he’s appeared in.