n execute next line s execute next statement f finish current method call and

N execute next line s execute next statement f finish

This preview shows page 159 - 161 out of 517 pages.

n execute next line s execute next statement f finish current method call and return p expr print expr , which can be anything that’s in scope within the current stack frame eval expr evaluate expr ; can be used to set variables that are in scope, as in eval x=5 up go up the call stack, to caller’s stack frame down go down the call stack, to callee’s stack frame where display where you are in the call stack b file:num set a breakpoint at line num of file (current file if file: omitted) b method set a breakpoint when method called c continue execution until next breakpoint q quit program Figure 4.12: Command summary of the interactive Ruby debugger. That brings us to the third question posed at the beginning of Section 4.6 : what view should we display when the create action completes? To be consistent with other actions like show , we could create a view app/views/movies/create.html.haml containing a nice message informing the user of success, but it seems gratuitous to have a separate view just to do that. What most web apps do instead is return the user to a more useful page—say, the home page, or the list of all movies—but they display a success message as an added element on that page to let the user know that their changes were successfully saved.
Image of page 159
Rails makes it easy to implement this behavior. To send the user to a different page, redirect_to causes a controller action to end not by rendering a view, but by restarting a whole new request to a different action. Thus, redirect_to movies_path is just as if the user suddenly requested the RESTful Index action GET movies (that is, the action corresponding to the helper movies_path ): the index action will run to completion and render its view as usual. In other words, a controller action must finish by either rendering a view or redirecting to another action. Remove the debug breakpoint from the controller action (which you inserted if you modified your code according to Screencast 4.7.1 ) and modify it to look like the listing below; then test out this behavior by reloading the movie listing page, clicking Add New Movie, and submitting the form. 1 # in movies_controller.rb 2 def create 3 @movie = Movie.create!(params[:movie]) 4 redirect_to movies_path 5 end Of course, to be user-friendly, we would like to display a message acknowledging that creating a movie succeeded. (We’ll soon deal with the case where it fails.) The hitch is that when we call redirect_to , it starts a whole new HTTP request; and since HTTP is stateless, all of the variables associated with the create request are gone. To address this common scenario, the flash[] is a special method that quacks like a hash, but persists from the current request to the next. (In a moment we’ll explore how Rails accomplishes this.) In other words, if we put something into flash[] during the current controller action, we can access it during the subsequent action. The entire hash is persisted, but by convention, flash[:notice] is used for informational messages and flash[:warning] is used for messages about things going wrong.
Image of page 160
Image of page 161

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