name in this case newhtmlhaml so you can just add the following trivial new

Name in this case newhtmlhaml so you can just add the

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

name (in this case new.html.haml ), so you can just add the following trivial new method to movies_controller.rb : 1 def new 2 # default: render ’new’ template 3 end Rails makes it easy to describe a fill-in form using form tag helpers available to all views. Put the code in Figure 4.11 into app/views/movies/new.html.haml and watch Screencast 4.6.1 for a description of what’s going on in it. 1 %h2 Create New Movie 2 3 = form_tag movies_path, :method => :post do 4 5 = label :movie, :title, ’Title’ 6 = text_field :movie, :title 7 8 = label :movie, :rating, ’Rating’ 9 = select :movie, :rating, [’G’,’PG’,’PG-13’,’R’,’NC-17’] 10 11 = label :movie, :release_date, ’Released On’ 12 = date_select :movie, :release_date 13 14 = submit_tag ’Save Changes’ Figure 4.11: The form the user sees for creating and adding a new movie to RottenPotatoes. Screencast 4.6.1: Views with fill-in forms The form_tag method for generating a form requires a route to which the form should be submitted— that is, a URI and an HTTP verb. We use the RESTful URI helper and HTTP POST method to generate a route to the create action, as rake routes reminds us. As the screencast mentions, not all input field types are supported by the form tag helpers (in this case, the date fields aren’t supported), and in some cases you need to generate forms whose fields don’t necessarily correspond to the attributes of some ActiveRecord object. To recap where we are, we created the new controller method that will render a view giving the user a form to fill in, placed that view in new.html.haml , and arranged to have the form submitted to the create controller method. All that remains is to use the information in params (the form field values)
Image of page 157
to actually create the new movie in the database. Summary Rails provides form helpers to generate a fill-in form whose fields are related to the attributes of a particular type of ActiveRecord object. When creating a form, you specify the controller action that will receive the form submission by passing form_tag the appropriate RESTful URI and HTTP method (as displayed by rake routes ) . When the form is submitted, the controller action can inspect params[] , which will contain a key for each form field whose value is the user-supplied contents of that field. Self-Check 4.6.1. In line 3 of Figure 4.11 , what would be the effect of changing :method=>:post to :method=>:get and why? The form submission would result in listing all movies rather than creating a new movie. The reason is that a route requires both a URI and a method. As Figure 4.7 shows, the movies_path helper with the GET method would route to the index action, whereas the movies_path helper with the POST method routes to the create action. Self-Check 4.6.2. Given that submitting the form shown in Figure 4.11 will create a new movie, why is the view called new.html.haml rather than create.html.haml ?
Image of page 158
Image of page 159

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