Shenrak
Hi everyone,
I am trying to use Ecto.insert, and i have the following error :
[error] #PID<0.416.0> running GORprojectWeb.Endpoint (cowboy_protocol) terminated
Server: localhost:4000 (http)
Request: POST /api/rooms
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in GORprojectWeb.ErrorHelpers.translate_error/1
(gor_project) lib/gor_project_web/views/error_helpers.ex:9: GORprojectWeb.ErrorHelpers.translate_error("is invalid")
(ecto) lib/ecto/changeset.ex:2345: anonymous fn/3 in Ecto.Changeset.merge_error_keys/3
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/changeset.ex:2339: Ecto.Changeset.traverse_errors/2
(gor_project) lib/gor_project_web/views/changeset_view.ex:17: GORprojectWeb.ChangesetView.render/2
(phoenix) lib/phoenix/view.ex:332: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/controller.ex:740: Phoenix.Controller.do_render/4
(gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.action/2
(gor_project) lib/gor_project_web/controllers/room_controller.ex:1: GORprojectWeb.RoomController.phoenix_controller_pipeline/2
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.plug_builder_call/2
(gor_project) lib/gor_project_web/endpoint.ex:1: GORprojectWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/giom/Documents/develop/web/GORProject-API/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
After some investigations, it seems that the error comes from an association not loaded, but the errors kind of triggers me because i have in my errors.pot the strings to handle this error :
## From Ecto.Changeset.put_change/3
msgid "is invalid"
msgstr ""
So i should be handled.
Here are my schemas, CRUD and controller :
Schema :
defmodule GORproject.Rooms.Room do
use Ecto.Schema
import Ecto.Changeset
alias GORproject.Rooms.Room
alias GORproject.Auth.User
schema "rooms" do
field(:depth, :integer)
field(:name, :string)
field(:description, :string)
field(:public, :boolean)
many_to_many(:users, User, join_through: "users_rooms")
has_many(:children, Room)
belongs_to(:father, Room, foreign_key: :father_id)
timestamps()
end
@doc false
def changeset(room, attrs) do
room
|> cast(attrs, [:name, :depth, :description, :public, :father_id])
|> foreign_key_constraint(:father_id)
|> validate_required([:name, :depth, :public])
end
end
CRUD :
def create_room(attrs \\ %{}) do
%Room{children: [], father: {}, users: []}
|> Room.changeset(attrs)
|> Repo.insert()
end
Controller :
def create(conn, %{"room" => room_params}) do
with {:ok, %Room{} = room} <- Rooms.create_room(room_params) do
conn
|> put_status(:created)
|> put_resp_header("location", room_path(conn, :create, room))
|> render("show.json", room: room)
end
end
Thank you ![]()
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
script
How you handling in create method if changeset is not valid?
Shenrak
error is sent to my fallbackController :
script
Fall back controller is okay but its better if you use the error_view file for displaying changeset errors.
you can pass your invalid changeset to this and it will display an error from that changeset .
Shenrak
I added your code in my error_view.ex, and it seems like the error is not handled there. From the stacktrace it seems that it goes from room_controller directly to changeset_view, in which there is :
script
From stacktrace it seems like in
error helpersthe translate_error function is not working for youris invalidmessage. You are passing one argument but it accepts 2 arguments.Shenrak
I totally agree with you, and this is where i do not understand. In my
errors.pot, i have the following code :According to this thread, it should be working
script
what is your goal? What you trying to implement?
Shenrak
I am trying to understand why this error is raised, how to handle it properly, and of course i am trying to fix the Repo.insert error
note that the error is not raised anymore if i delete
father: {}insideBut i then have the following error :
script
Add
IO.inspectafter|> Room.changeset(attrs)And see what it gives you in the console. After you run this method
Shenrak
i’ve put two IO.inspect :
and i have :
followed by the error stacktrace