We want to use our content editable, taking advantage of the generated routes with the following in router.ex:
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", TextEditor do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
resources "/posts", PostController
end
and the controller functions, namely create:
def create(conn, %{"post" => post_params}) do
changeset = Post.changeset(%Post{}, post_params)
case Repo.insert(changeset) do
{:ok, _post} ->
conn
|> put_flash(:info, "Post created successfully.")
|> redirect(to: post_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
However we don't want to use the generated form, we are then attempting to test this with divs and the jquery method $.post:
<div id="newPost" contenteditable="true">write here</div>
<div id="button" class="btn btn-primary">Save</div>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function() {
var post = $("#newPost").html();
$.post( "/posts/post", { title: post })
.done(function() {
alert( "Data Loaded: " );
});
});
});
</script>
However we are not getting the alert or any data inserted into our database. What are we missing?
EDIT: In the browser pipeline we removed the cross forgery plug because of a csrf token error.
browser
pipeline? – Gazler[info] POST /posts/post
. I added the browser pipeline to the question. We removed the cross forgery plug because of the csrf token error. – mesosteros:put_secure_browser_headers
? – Gazler