rb which was auto generated by the rails new command and is heavily commented

Rb which was auto generated by the rails new command

This preview shows page 138 - 140 out of 517 pages.

config/routes.rb , which was auto-generated by the rails new command and is heavily commented. Replace the contents of the file with the following (the file is mostly comments, so you’re not actually deleting much): 1 Myrottenpotatoes::Application.routes.draw do 2 resources :movies 3 root :to => redirect(’/movies’) 4 end Very important: In addition, delete the file public/index.html if it exists. Save the routes.rb file and run rake routes again, and observe that because of our change to routes.rb , the first line of output says that the URI GET /movies will try to call the index action of the movies controller; this and most of the other routes in the table are the result of the line resources :movies , as we’ll soon see. The root route ’/’ , RottenPotatoes’ “home page,” will take us to the main Movie listings page by a mechanism we’ll soon see called an HTTP redirect . Symbol or string? As with many Rails methods, resources ’movies’ would also work, but idiomatically, a symbol indicates that the value is one of a fixed set of choices rather than an arbitrary string. Using convention over configuration, Rails will expect this controller’s actions to be defined in the class MoviesController , and if that class isn’t defined at application start time, Rails will try to load it from the file app/controllers/movies_controller.rb . Sure enough, if you now reload the page in your browser, you should see a different error: uninitialized constant MoviesController . This is good news: a Ruby class name is just a constant that refers to the class object, so Rails is essentially complaining that it can’t find the MoviesController class, indicating that our route is working correctly! As before, this error message and additional information are captured in the log file log/development.log . Having covered the first two steps in the list—setting up the app skeleton and creating some initial routes—we can move on to setting up the database that will store the models, the “M” of MVC. Summary: You used the following commands to set up a new Rails app: rails new sets up the new app; the rails command also has subcommands to run the app locally with WEBrick ( rails server ) and other management tasks. Rails and the other gems your app depends on (we added the Haml templating system and the Ruby debugger) are listed in the app’s Gemfile , which Bundler uses to automate the process of creating a consistent environment for your app whether in development or production mode. To add routes in config/routes.rb , the one-line resources method provided by the Rails routing system allowed us to set up a group of related routes for CRUD actions on a RESTful resource. The log files in the log directory collect error information when something goes wrong.
Image of page 138
ELABORATION: Automatically reloading the app You may have noticed that after changing routes.rb , you didn’t have to stop and restart the app in order for the changes to take effect. In development mode, Rails reloads all of the app’s classes on every new request, so that your changes take effect
Image of page 139
Image of page 140

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors