laurenfackler

laurenfackler

Dev-only routes

To quickly test changes to email template copy and styling, I created a couple routes that are only accessible in the dev environment:

if Mix.env() == :dev do
  get("/email_previews", EmailPreviewController, :index)
  get("/email_previews/:email", EmailPreviewController, :show)
end

However, this now means that there is a template file that references a path helper function that doesn’t exist in other environments:

<ul>
<%= for email <- @emails do %>
  <li><a href="<%= email_preview_path(@conn, :show, to_string(email)) %>"><%= email %></a></li>
<% end %>
</ul>

which prevents the app from compiling:

** (CompileError) lib/my_app_web/templates/email_preview/index.html.eex:9: undefined function email_preview_path/3

How can I get the app to compile?

Marked As Solved

andrejsm

andrejsm

Simply don’t use the compiled path functions. Instead type them as strings and interpolate values.

Also Liked

mudasobwa

mudasobwa

Creator of Cure

When one needs different modules to be compiled in different environments, it’d be better to explicitly define these module sets, instead of polluting the source code with conditionals.

For the modules to be available in :test only, the common approach is to create test/support folder, and modify your mix.exs in the following way:

  1. Create a private function like this:
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
  1. Modify your Mix.Project.project/0 callback to include the following key/value pair:
elixirc_paths: elixirc_paths(Mix.env())
  1. Put all the files containing modules needed only in test environment there.

That way all of them will be compiled in test and discarded in all other environments.

If you need some modules to exist in :prod only, declare compile paths appropriately.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right, the if Mix.env == :dev do call in the router happens at compile time because it’s in the module body.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You don’t want to do Mix.env calls at runtime. Does the email_preview_path exist in a view that only is needed in dev?

Last Post!

laurenfackler

laurenfackler

Right, yes. I am already using that feature of Swoosh. However, I wanted a slightly different solution where I could quickly preview any email without having to manually go through the user flows within my application (e.g. filling out forms). Unless, I’m completely missing something in the README…

What I created, allows me to visit http://localhost:4000/email_previews and see a list of links to preview all my apps emails:

  • appointment_booked_patient
  • appointment_booked_practice
  • confirmation
  • duplicate_locations
  • new_app_version
  • password
  • unlock
  • user_registered_admin
  • welcome
  • etc

I also created a gist for anyone else who might stumble across this thread and want the same:

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement