Skip to content

Instantly share code, notes, and snippets.

@RichMorin
Last active November 25, 2018 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichMorin/bfba8480d144a9daf11a8bed44176f07 to your computer and use it in GitHub Desktop.
Save RichMorin/bfba8480d144a9daf11a8bed44176f07 to your computer and use it in GitHub Desktop.
need help creating a form
I'm using Phoenix 1.4 and trying to create a form that has
several dozen sets of radio buttons. The Phoenix.HTML.Form
helpers seem to assume that I'm using a database, which I'm
not. So, I coded up HTML for the headings and input widgets.
This displayed nicely, but when I tried to submit the form
(using <form ...>), I got a nastygram about CSRF. I tried
using form_for, but that gives me the following warning:
warning: function PhxHttpWeb.Router.Helpers.page_path/2
is undefined or private. Did you mean one of:
* text_path/2
* text_path/3
Suggestions, anyone?
<%= form_for @conn, Routes.page_path(@conn, :find_show), fn _f -> %>
<%= for tag_type <- tag_types(@tag_info) do %>
<h3><%= tag_type %></h3>
<ul>
<%= sub_map = @tag_info[tag_type]
tag_vals = RefData.Common.keyss(sub_map)
for tag_val <- tag_vals do %>
<% tmp = String.replace(tag_val, ~r{\W+}, "_")
name = "#{ tag_type }_#{ tmp }"%>
<li>
<%= tag_val %> (<%= sub_map[tag_val] %>)<br>
<input type="radio" id="a_<%= name %>"
name="<%= name %>" value="a">
<label for="a_<%= name %>">-2</label> &nbsp;
<input type="radio" id="b_<%= name %>"
name="<%= name %>" value="b">
<label for="b_<%= name %>">-1</label> &nbsp;
<input type="radio" id="c_<%= name %>"
name="<%= name %>" value="c" checked>
<label for="c_<%= name %>">0</label> &nbsp;
<input type="radio" id="d_<%= name %>"
name="<%= name %>" value="d">
<label for="d_<%= name %>">1</label> &nbsp;
<input type="radio" id="e_<%= name %>"
name="<%= name %>" value="e">
<label for="e_<%= name %>">2</label>
</li>
<% end %>
</ul>
<% end %>
<input type="submit" value="Submit">
<% end %>
Here is the router.ex file. Note that the controller module names
(eg, ArticlesController, TextController) are my own:
defmodule PhxHttpWeb.Router do
use PhxHttpWeb, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", PhxHttpWeb do
pipe_through :browser
get "/", TextController, :home
get "/about", TextController, :about
get "/articles/item", ArticlesController, :item
get "/articles/main", ArticlesController, :main
get "/articles/type", ArticlesController, :type
get "/resources/find", ResourcesController, :find_form
post "/resources/find", ResourcesController, :find_show
get "/resources/item", ResourcesController, :item
get "/resources/main", ResourcesController, :main
get "/resources/type", ResourcesController, :type
get "/source", SourceController, :show
end
# Other scopes may use custom stacks.
# scope "/api", PhxHttpWeb do
# pipe_through :api
# end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment