This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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.
The 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.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up