Learn
Associations I
Show a destination

Nice job! The app displays all destinations that belong to a tag. How does this work?

  • When a user visits http://localhost:8000/tags/1, the route get '/tags/:id' => 'tags#show' sends this request to the Tags controller’s show action with {id: 1} in params.
  • The @destinations = @tag.destinations retrieves all the destinations that belong to the tag, and stores them in @destinations. The has_many / belongs_to association lets us query for destinations like this.
  • The tag and its destinations are sent to the view to be displayed.

Instructions

1.

Let’s add functionality to see a destination.

Generate a controller named Destinations.

2.

Next in the routes file, map requests to/destinations/:id to the Destinations controller’s show action. Use as: to name this route “destination”.

3.

Then in the Destinations controller, set up the show action. Use params to find a destination by id, and save it in @destination

4.

Then in the view app/views/destinations/show.html.erb, display the destination’s image, name, and description.

5.

Finally in app/views/tags/show.html.erb below a destination’s description, use link_to to add a link to that destination:

  • Use “See more” for the link text
  • By giving the show route a name of “destination”, Rails automatically creates a helper method named destination_path. Use destination_path to generate a URL to a specific destination’s path.
6.

Visit http://localhost:8000/tags/1 in the browser. Click on a destination to see its show page.

Folder Icon

Sign up to start coding

Already have an account?