Skip to content

Instantly share code, notes, and snippets.

@kareemgrant
Last active August 29, 2015 14:22
Show Gist options
  • Save kareemgrant/8f75efb3facdaeefd726 to your computer and use it in GitHub Desktop.
Save kareemgrant/8f75efb3facdaeefd726 to your computer and use it in GitHub Desktop.

Displaying Database Data in your Sinatra Application

Goals

  1. Understand how you can take information from your database and display it to your users

  2. Create a has_many relationship in your application

Assignment

  1. Continue using the sinatra_blog you created during the last homework assignment. Add a posts table to your application (remember: you will need to use migrations) and establish a has_many relationship between users and posts, where users can have many posts. The posts table should have three fields: title, body and a foreign key.
  2. Once you have established the has_many relationship at the database level, you will also need to communicate the relationship at the application level. After that is done, use irb and activerecord to add posts to users.
  3. In your application, add a new route get '/users' that will display a list of all of the users in your database
  4. Next, create a another route get '/users/:id that will display the information for only one user. For example - by typing localhost:4567/users/3 in your address bar, you should be redirected to a page that only displays the information for the user in your database that has an id of 3.
  5. On the detail page (from the step above) - display the name and email of the user. Also, display a list of the post titles they have

Hint: We went through these steps using the sinatra_todo application, so reference that application to guide your through this assignement

VERY IMPORTANT: If you haven't already done so; please ensure that your laptop is correctly set up for Rails development by following the setup instructions found on installrails.com. If this is not completed before next class, you will run into issues and you may fall behind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment