View for New Pages5:42 with Jay McGavren
We have the /pages/new path routed to the PagesController's new method. In that method, we've set up a new Page object in the @page instance variable. Now, we need to set up an ERB view template that creates a form based on that Page object.
form_for method takes a model object, and generates an HTML form for it.
# This will embed the form HTML in the output. # Here, a form will be generated for the object # in @page. <%= form_for(@page) do |f| %> <div class="field"> # This will output a label for the "title" field. <%= f.label :title %> # This will output a text field used to set the # title attribute of the Page object. <%= f.text_field :title %> </div> <div class="field"> # This will output a label for the "body" field. <%= f.label :body %> # This will output a larger text field used to set the # body attribute of the Page object. <%= f.text_area :body %> </div> <div> # Outputs a button used to submit the form. <%= f.submit %> </div> <% end %>
You can read more about
form_for in the official Rails documentation.
Want to learn more about HTML forms? Check out our course on the topic.
You need to sign up for Treehouse in order to download course files.Sign up