niccolox

niccolox

I am trying to create a Phoenix controller action that allows conn to fall through action if the parameter is not matched by a query.

i.e. if there is no user the conn passes through this action and gets caught by another controller

I am getting an error 500.

What am I missing?

def pass_action(conn, %{"id" => id}) do
     user = Repo.get_by(User, slug: id)
     if user != nil do
        user = Repo.get_by!(User, slug: id)
        render(conn, "show.html",
          user: user)
      else
         conn
      end
end

this status 500

00:20:16.059 [error] #PID<0.23653.0> running WwwAppCom.Web.Endpoint (connection #PID<0.21719.0>, stream id 13) terminated
Server: local.app.com:4000 (http)
Request: GET /usr
** (exit) an exception was raised:
    ** (Plug.Conn.NotSentError) a response was neither set nor sent from the connection
        (phoenix 1.5.8) lib/phoenix/endpoint/cowboy2_handler.ex:110: Phoenix.Endpoint.Cowboy2Handler.maybe_send/2
        (phoenix 1.5.8) lib/phoenix/endpoint/cowboy2_handler.ex:66: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.8.0) /deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.8.0) /deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
        (cowboy 2.8.0) /deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
        (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Showing Posts 1 to 4

matthieuchabert

matthieuchabert

Hi,

In a controller, you need to send a response back to the client. For example, your show.html template, a 404, a redirect, etc.

If I understand correctly you would want to use a redirect: redirect(conn, to: "/your-other-controller") instead of just conn.

I hope it fixes your issue.

LostKobrakai

LostKobrakai

That’s not possible with the existing router implemenations. Once a controller was matched by the router the request is neither going back into the router, nor could the router match a later controller, as routers work with pattern matching behind the scenes. The beam does not expose means of “skipping” such a match.

axelson

axelson

Scenic Core Team

I believe you can accomplish what you want with a fallback controller: Phoenix.Controller — Phoenix v1.8.8

niccolox

niccolox OP

thanks @matthieuchabert @LostKobrakai @axelson

I will investigate this very helpful information

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews