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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement