Learn
Associations I
Display all tags

Nice work. What did we just do?

  1. We added two string columns title and image to the tags table
  2. We added three string columns name, image, and description to the destinations table
  3. We also added the line t.references :tag to the destinations table. This adds a foreign key pointing to the tags table.
  4. Finally we ran the migrations to update the database, and seeded the database with the data in db/seeds.rb.

Instructions

1.

Now that the models are set up, let’s move on to the rest of the request/response cycle and create a controller, a route, and a view.

Generate a controller named Tags

2.

In the routes file, add a new route that maps requests to /tags to the Tags controller’s index action.

3.

Then in the Tags controller, add the index action to display a list of all tags. To do this, fetch all tags from the database and store them in variable @tags.

4.

In app/views/tags/index.html.erb at line 12, iterate through each tag in the @tags array. Then for each tag, display its title and image.

We’ve provided CSS in app/assets/stylesheets/application.css.

5.

Visit http://localhost:8000/tags in the browser.

Folder Icon

Sign up to start coding

Already have an account?