goals do
goal "creat a HTML form using Rails form helpers"
goal "Learn to read the Rails server output"
goal "Update the controller so that the form saves submissions to the database"
goal "Learn how to handle parameters"
end
message <<-MARKDOWN
# Side Note: Web forms are not perfectly straightfoward
They're like, the most basic thing about the internet, other than cat gifs, right? So they should be straightforward, right? WRONG! They are exciting and just complicated enough to bake your noodle a bit.
But don't worry, Rails has strong opinions about forms, so we won't have to do _too_ much typing.
# Setting up the page for the form
Let's take a look at our handy routes, which can help us figure out what we need to do. We're going to follow the same pattern that we did for the main `/jobs` page.
First, visit the routing page at <http://localhost:3000/rails/info>, and find the route for `/jobs/new`.
That's the one we want for our form. Let's visit that page and see what it says
MARKDOWN
steps do
step "Visit /jobs/new" do
message "open <http://localhost:3000/jobs/new> in your browser"
error_box "The action 'new' could not be found for JobsController"
message "This looks familiar! do you remember the next step?"
end
step "add an action to the controller" do
source_code_with_message "Let's add a method to our jobs controller called `new`:", :ruby,
<<-RUBY
def new
@job = Job.new
end
RUBY
message "refresh <http://localhost:3000/jobs/new> in your browser"
error_box "JobsController#new is missing a template for request formats: text/html"
message "do you remember how to add the template? Which file do you need to create?"
end
step "create a view" do
message "Under app/views/jobs, add a file called new.html.erb. This will be our form. Add some html to the template to keep things moving along:"
source_code :html,
<<-HTML
<h1>Add a job</h1>
HTML
message "Refresh again: <http://localhost:3000/jobs/new>"
message "there should be no error now"
end
step "Add a form!" do
message "Rails has handy helpers for forms. We'll be using a form helper specifically made for use with a model."
source_code_with_message "In the view file you were just editing (app/views/jobs/new), add the following code:", :erb,
<<-ERB
<%= form_with(model: @job, local: true) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :description %>
<%= form.text_area :description, size: '60x6' %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
ERB
message "Save that file, and reload the page."
message "Now we should see our mostly unstyled form!"
end
end
commitnow
discussion_box "Form HTML", "What HTML did the form helpers produce? Using the web inspector, look through the form code and compare it to the file you've been working on in VS Code."
next_step "make_the_form_work"