Exploit automatic indentation and syntax highlighting If your text editor

Exploit automatic indentation and syntax highlighting

This preview shows page 154 - 156 out of 517 pages.

Exploit automatic indentation and syntax highlighting. If your text editor insists on indenting a line farther than you want it to be indented, you may have forgotten to close a parenthesis, brace, or
Image of page 154
do...end block somewhere upstream, or you may have forgotten to “escape” a special character (for example, a single-quote inside a single-quoted string). If your editor isn’t so equipped, you can either write your code on stone tablets, or switch to one of the more productive modern editors suggested in Appendix A.3 . Look in the log file, usually log/development.log , for complete error information including the backtrace. In production apps, this is often your only alternative, as Rails apps are usually configured to display a more user-friendly error page in production mode, rather than the error backtrace you’d see if the error occurred in development mode. In the second kind of problem, the app runs but produces an incorrect result or behavior. Most developers use a combination of two approaches to debug such problems. The first is to insert instrumentation —extra statements to record values of important variables at various points during program execution. There are various places we can instrument a Rails SaaS app—try each of the below to get a feel for how they work: printf debugging is an old name for this technique, from the C library function that prints a string on the terminal. Display a detailed description of an object in a view. For example, try inserting = debug(@movie) or = @movie.inspect in any view (where the leading = tells Haml to execute the code and insert the result into the view). “Stop the show” inside a controller method by raising an exception whose message is a representation of the value you want to inspect, for example, raise params.inspect to see the detailed value of the params hash inside a controller method. Rails will display the exception message as the Web page resulting from the request. Use logger.debug( message ) to print message to the log. logger is available in models and controllers and can record messages with a variety of urgencies; compare config/environments/production.rb with development.rb to see how the default logging level differs in production vs. development environments. The second way to debug correctness problems is with an interactive debugger. We already installed the debugger gem via our Gemfile; to use the debugger in a Rails app, start the app server using rails server --debugger , and insert the statement debugger at the point in your code where you want to stop the program. When you hit that statement, the terminal window where you started the server will give you a debugger prompt. In Section 4.7 , we’ll show how to use the debugger to shed some light on Rails internals.
Image of page 155
Image of page 156

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