Skip to content

Instantly share code, notes, and snippets.

@maxgronlund
Last active May 1, 2016 14:02
Show Gist options
  • Save maxgronlund/59d9438ef5986560c647628ca07832b3 to your computer and use it in GitHub Desktop.
Save maxgronlund/59d9438ef5986560c647628ca07832b3 to your computer and use it in GitHub Desktop.
The controller
Compiled web/channels/room_channel.ex
[info] POST /api/v1/like_posts
[debug] Processing by MusicTester.LikePostController.create/2
Parameters: %{"like_post" => "{ post_id: 15, user_id: 1}"}
Pipelines: [:api]
"{ post_id: 15, user_id: 1}"
[info] Sent 500 in 7ms
[error] #PID<0.898.0> running MusicTester.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/v1/like_posts
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in Ecto.Changeset.cast/5
(ecto) lib/ecto/changeset.ex:361: Ecto.Changeset.cast(%MusicTester.LikePost{__meta__: #Ecto.Schema.Metadata<:built>, id: nil, inserted_at: nil, post: #Ecto.Association.NotLoaded<association :post is not loaded>, post_id: nil, updated_at: nil, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: nil}, %{}, "{ post_id: 15, user_id: 1}", [], [])
(music_tester) web/controllers/like_post_controller.ex:14: MusicTester.LikePostController.create/2
(music_tester) web/controllers/like_post_controller.ex:1: MusicTester.LikePostController.action/2
(music_tester) web/controllers/like_post_controller.ex:1: MusicTester.LikePostController.phoenix_controller_pipeline/2
(music_tester) lib/phoenix/router.ex:261: MusicTester.Router.dispatch/2
(music_tester) web/router.ex:1: MusicTester.Router.do_call/2
(music_tester) lib/music_tester/endpoint.ex:1: MusicTester.Endpoint.phoenix_pipeline/1
(music_tester) lib/plug/debugger.ex:93: MusicTester.Endpoint."call (overridable 3)"/2
(music_tester) lib/phoenix/endpoint/render_errors.ex:34: MusicTester.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
defmodule MusicTester.LikePost do
use MusicTester.Web, :model
schema "like_posts" do
belongs_to :user, MusicTester.User
belongs_to :post, MusicTester.Post
timestamps
end
@required_fields ~w()
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
Apex.ap params
model
|> cast(params, @required_fields, @optional_fields)
end
end
def create(conn, %{"like_post" => like_post_params}) do
changeset = LikePost.changeset(%LikePost{}, like_post_params)
case Repo.insert(changeset) do
{:ok, like_post} ->
conn
|> put_status(:created)
|> put_resp_header("location", like_post_path(conn, :show, like_post))
|> render("show.json", like_post: like_post)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(MusicTester.ChangesetView, "error.json", changeset: changeset)
end
= link gettext("Like"), to: "#", class: "like-post", id: @post.id, value: "{ post_id: #{@post.id}, user_id: #{ @current_user.id}}"
defmodule MusicTester.Router do
use MusicTester.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug MusicTester.Locale
plug MusicTester.Auth, repo: MusicTester.Repo
plug MusicTester.Navbar
plug MusicTester.Breadcrumbs
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/api/v1" do
pipe_through :api
resources "/like_posts", MusicTester.LikePostController
end
scope "/", MusicTester do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
resources "/users", UserController, except: [:index]
resources "/forgot_password", ForgotPasswordController, only: [:index, :create]
resources "/reset_password", ResetPasswordController, only: [:show, :update]
resources "/sessions", SessionController, only: [:new, :create, :delete]
resources "/posts", PostController, only: [] do
resources "/comments", CommentController, exclude: [:show, :index]
end
resources "/topics", TopicController, exclude: [:show] do
resources "/posts", PostController
end
resources "/comments", CommentController, only: [] do
resources "/replies", ReplyController, exclude: [:index]
end
resources "/works", WorkController, exclude: [:show] do
resources "/recordings", RecordingController
end
resources "/like_posts", LikePostController#, except: [:new, :edit]
end
# Other scopes may use custom stacks.
scope "/admin", MusicTester do
pipe_through [:browser, :authenticate_admin]
resources "/", AdminController, only: [:index]
resources "/users", UserController, only: [:index]
#pipe_through :api
resources "/banners", BannerController, except: [:show]
resources "/categories", CategoryController, except: [:show]
resources "/cards", CardController, except: [:show]
end
end
$(".like-post").click(function() {
let csrf = document.querySelector("meta[name=csrf]").content;
let params = $(this).attr('value')
$.ajax({
url: "/like_posts",
type: "POST",
params: {like_post: params},
data: {
like_post: params
},
headers: {
"X-CSRF-TOKEN": csrf
},
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment