Homework for 7A

Review

Topics Covered: Rails basics


Exercise

E1: Classroom App

This morning's exercise involves building a Rails app that will welcome you to the app and display a list of all the student's names in your class on a separate page. The app will have one controller, three routes, two views (and two corresponding methods/actions), two instance variables, an array, and the use of ERB to display the data from our instance variables on the view pages.

Features

Your classroom app will include the following:

(don't forget to use git to track your code, stage and commit after each step)

  1. One controller. See here for controller naming convention in Rails (hint: pluralize your controller name). The controller belongs in the controllers folder.

  2. Two methods/actions (to be defined inside of your controller), one for your index page (named index), and one for your students page (named students).

  3. Three routes (to locate your routes file use cmd + p (ctrl + p on PC) and type the name of the page: routes.rb). One route will point to your classrooms controller's index action (method), the other will point to your classrooms controller's students action. Don't forget to set index as your root for the third route. Type rake routes in your terminal to view your routes (Hint: if you're stuck, use the commented out text that lives inside of the routes.rb file as a guideline).

  4. Two views, one for your home (index) page, and another that will have a list of you and all your classmates (views are the files ending in .html.erb that will display our app on the client, they belong in the views folder).

  5. Go back to your controller now and include two instance variables, one for each method. The index action will have a welcome_message instance variable that contains a greeting. The students action will include a students instance variable that points to an array that holds the names of all the students in your class, including you.

  6. Use ERB syntax to display the data from your instance variables in their corresponding views. Try using an unordered list (<ul></ul>) for the array (Hint: You will need to use a method to enumerate through your array and display its contents).

  7. BONUS: Create links from your index page to your students page and back again using the link_to helper (See here for some documentation on using link_to in Rails)

  8. Add, Commit, and Push your app to your GitHub's exercises folder.