default look for an instance variable whose name matches their first argumentin

Default look for an instance variable whose name

This preview shows page 164 - 166 out of 517 pages.

default look for an instance variable whose name matches their first argument—in this case, the first argument is :movie so the text_field helper will look for a variable @movie . If it exists and corresponds to an ActiveRecord model, the helper assumes that this form is for editing an existing object, and @movie ’s current attribute values will be used to populate the form fields. If it doesn’t exist or doesn’t respond to the attribute method in the second argument ( ’title’ ), the form fields will be blank. This behavior is a good reason to name your instance variable @movie rather than (say) @my_movie : you can still get the extra functionality from the helpers, but you’ll have to pass extra arguments to them. The last CRUD action is Delete, which Figure 4.4 shows can be accomplished by calling destroy on an ActiveRecord model. As with the Update action, it’s common practice to respond to a Delete by destroying the object and then returning the user to some other useful page (such as the Index view) and displaying a confirmation message that the item was deleted, so we already know how to write the controller method—add the following lines to movies_controller.rb : 1 def destroy 2 @movie = Movie.find(params[:id]) 3 @movie.destroy 4 flash[:notice] = "Movie ’#{@movie.title}’ deleted." 5 redirect_to movies_path 6 end (Recall from the explanation accompanying Figure 4.4 that even after destroying an object in the database, the in-memory copy can still be queried for its attributes as long as we don’t try to modify it or ask it to persist itself.) As we did with Edit, we’ll provide access to the Delete action from each movie’s Show page. What kind
Image of page 164
of HTML element should we use? rake routes tells us the Delete action requires the URI /movies/:id with the HTTP verb DELETE . This seems similar to Edit, whose URI is similar but uses the GET method. Since we’ve been using the URI helpers to generate routes, we can still use link_to in this case, but its behavior is a bit different from what you might expect. 1 -# Our Edit link from previous example: 2 = link_to ’Edit info’, edit_movie_path(@movie) 3 -# This Delete link will not really be a link, but a form: 4 = link_to ’Delete’, movie_path(@movie), :method => :delete If you examine the HTML generated by this code, you’ll find that Rails generates a link that includes the unusual attribute data-method=”delete” . Long before RESTfulness became a prominent SaaS concept, there was already a general guideline that SaaS app requests that used GET should always be “safe”—they should not cause any side effects such as deleting an item or purchasing something, and should be safely repeatable. Indeed, if you try to reload a page that resulted from a POST operation, most browsers will display a warning asking if you really want to resubmit the form.
Image of page 165
Image of page 166

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