gilbertbw

gilbertbw

Using conditionally compiled routes with verified routes

I have added a page that is only available in development

defmodule MyWeb.Router do
  use MyWeb, :router

  ...
  
  # Enable LiveDashboard and Swoosh mailbox preview in development
  if Application.compile_env(:tax_comps, :dev_routes) do
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through :browser

      get "/myroute", MyWeb.DevMyRouteController, :blah
      post "/myroute", MyWeb.DevMyRouteController, :blah
    end
  end
end

this renders a form

<.simple_form :let={_form} for={@conn.params["blah_request"]} action={~p"/dev/myroute"} >
  <.input type="textarea" name="stuff" value={@stuff} label="Stuff?"/>
  <:actions>
      <.button name="separate" value="false">Go</.button>
  </:actions>
</.simple_form>

When running mix test --force I am getting

warning: no route path for MyWeb.Router matches "/dev/myroute"

I belive I could use the route helpers but I see this is deprecated, and in my Phoenix 1.17 project these helpers are disabled by default.

What is the recommended way to fix this?

Marked As Solved

josevalim

josevalim

Creator of Elixir

You can always use non-verified routes for those, by removing the ~p sigil, especially if they are dev only (so any mistake only affects dev, which you can quickly address).

Also Liked

sodapopcan

sodapopcan

Oops sorry that was a very unfortunate typo. I meant make them UNavailable in production, ie, they will work in any other env. ie if Mix.env() != :prod, do: # ....

D4no0

D4no0

As @sodapopcan mentioned, checking that the env is not prod is a very good solution.

To formulate on why you are getting this warning: this is due the fact that you have defined dev_routes only in dev.exs configuration. The tests are not using dev configuration by default, but its specific one that is test.exs, hence making those routes unavailable in the test environment.

As an alternative to the solution that was provided above, you could add dev_routes and set it to true in your test configuration, however I would always go with the solution that checks that the env is not prod, as it has much smaller footprint and it is times more readable.

As a word of warning, if this warning comes from your application code (as opposed to your tests) then you are most probably doing something wrong, you shouldn’t use environment specific routes in application code, as that would imply that the functionality using it will not work correctly across all environments.

sodapopcan

sodapopcan

This doesn’t seem like a great practice to me. In this case it’s perhaps harmless, but it doesn’t set a good precedent as it means test is inconsistent with dev in non-obvious ways. Perhaps I’m overthinking it :thinking:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement