Creating the new action in the controller

Right now, those routes won't work, because they don't yet have a controller action associated with them. Let's hop into our poll_controller.ex file and create the new action:

def new(conn, _params) do
poll = Vocial.Votes.new_poll()

conn
|> put_layout("special.html")
|> render "new.html", poll: poll
end

First, we start off by creating a new blank poll object. We'll use a function in our context that doesn't actually exist yet. We want to keep our context as the main method of interaction between our controller and our database, so we'll just pick a function name and use that later. Next, we'll basically just copy our index function over from the controller and use that code.

One thing you might notice is that we actually already have a lot of duplication in our code. We should start off by just replacing the default layout that comes with Phoenix with our special.html layout, so let's delete lib/vocial_web/templates/layout/app.html.eex and rename lib/vocial_web/templates/layout/special.html.eex to app.html.eex. Then, we can delete the put_layout(“special.html”) lines from our code. At the top of the PollController, we should also add an alias Vocial.Votes line to make that code a little cleaner. Our new controller should look like this:

defmodule VocialWeb.PollController do
use VocialWeb, :controller
alias Vocial.Votes

def index(conn, _params) do
polls = Votes.list_polls()
render conn, "index.html", polls: polls
end

def new(conn, _params) do
poll = Votes.new_poll()
render conn, "new.html", poll: poll
end
end

We also need to build out the new function in our Context. We need it to create a blank changeset for us to start working with, so let's open up lib/vocial/votes/votes.ex and add the following function:

def new_poll do
Poll.changeset(%Poll{}, %{})
end

Now let's create the new.html template that our app is using. Create lib/vocial_web/templates/poll/new.html.eex and just give it a quick "hello" line. Now, if we pull up /polls/new in our browser, we should see everything that we created!

We can now start building up our forms and start using them! We will use some of Phoenix's built-in HTML helpers to help us put our poll form together. So in our new template, we have a quick header and we'll wrap everything inside of a div to be a good web developer citizen. Now we need to create a form that we can use to post our changes to the create route. Next, we'll use another helper to help us build a text box for our title attribute. The options portion is going to be a little bit trickier. We'll pick the simplest implementation path to start by just saying that each poll choice should be separated by a comma. This means we'll expect the data to come into our controller in the format "One, Two, Three". Finally, we'll need a submit button to bring it all back together at the end:

<div class="poll-form">
<h2>New Poll</h2>
<%= form_for @poll, poll_path(@conn, :create), fn f -> %>
<label>
Title:<br />
<%= text_input f, :title %>
</label>
<br />
<label>
Options (separated by commas):<br />
<input type="text" name="options" />
</label>
<br />
<%= submit "Create" %>
<% end %>
</div>

If we go back to our browser, we should see our nice new created form! Perfect! Now, we need to verify that it actually works, as writing all of this code isn't really worth much if we cannot demonstrate that it is all functional.

A workflow that you'll eventually settle into when you're building up your UI is keeping your controller and templates open at the same time. This is a good way to keep your code flowing logically from point A to point B

Let’s go back into our poll controller and piece together our create function!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset