Modify the controller action to store a useful message in the flash and try it

Modify the controller action to store a useful

This preview shows page 160 - 162 out of 517 pages.

is used for messages about things going wrong. Modify the controller action to store a useful message in the flash , and try it out: 1 # in movies_controller.rb 2 def create 3 @movie = Movie.create!(params[:movie]) 4 flash[:notice] = "#{@movie.title} was successfully created." 5 redirect_to movies_path 6 end What happened? Even though creating a new movie appears to work (the new movie shows up in the list of all movies), there’s no sign of the helpful message we just created. As you’ve probably guessed, that’s because we didn’t actually modify any of the views to display that message! But which view should we modify? In this example, we chose to redirect the user to the movies listing, so perhaps we should add code to the Index view to display the message. But in the future we might decide to redirect the user someplace else instead, and in any case, the idea of displaying a confirmation
Image of page 160
message or warning message is so common that it makes sense to factor it out rather than putting it into one specific view. Recall that app/views/layouts/application.html.haml is the template used to “wrap” all views by default. This is a good candidate for displaying flash messages since any pending messages will be displayed no matter what view is rendered. Make application.html.haml look like Figure 4.13 —this requires adding four lines of code between %body and =yield to display any pending flash messages at the beginning of the page body. 1 -# this goes just inside %body: 2 - if flash[:notice] 3 #notice.message= flash[:notice] 4 - elsif flash[:warning] 5 #warning.message= flash[:warning] Figure 4.13: Note the use of CSS for styling the flash messages: each type of message is displayed in a div whose unique ID is either notice or warning depending on the message’s type, but that share the common class message . This gives us the freedom in our CSS file to either style the two types of messages the same by referring to their class, or style them differently by referring to their IDs. Remarkably, Haml’s conciseness allows expressing each div ’s class and ID attributes and the message text to be displayed all on a single line. Try styling all flash messages to be printed in red text and centered. You’ll need to add the appropriate CSS selector(s) in app/assets/stylesheets/application.css to match the HTML elements that display the flash in the Application page template. The CSS properties color: red and text- align: center will get these effects, but feel free to experiment with other visual styles, colors, borders, and so on. If you do any nontrivial CSS work, you’ll want to use a dedicated CSS editor, such as the open-source and cross-platform Amaya or one of many commercial products. Summary Although the most common way to finish a controller action is to render the view corresponding to that action, for some actions such as create it’s more helpful to send the user back to a different view. Using redirect_to replaces the default view rendering with a redirection to a different action.
Image of page 161
Image of page 162

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