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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics 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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement