beam-community / bamboo

Testable, composable, and adapter based Elixir email library for devs that love piping.
https://hex.pm/packages/bamboo
MIT License
1.91k stars 345 forks source link

Templates not compiled maybe? #613

Closed AdamT closed 2 months ago

AdamT commented 3 years ago

I have non-traditional setup if I had to guess.

The EmailView looks like this:

defmodule MyApp.Notifications.Views.EmailView do
  use Bamboo.View, path: "lib/my_app/notifications/templates"
end

So I'm sending Emails from the context because I need to send them from a background job and so getting MyAppView involved didn't make sense to me. Anyhoo, the emails are sent from another module defined like:

defmodule MyApp.Notifications do
  use Bamboo.Template, view: MyApp.Notifications.Views.EmailView

  def reset_password_email(user) do
  ...
      base_email()
    |> to(email)
    |> subject("Forgot password")
    |> assign(:user, user)
    |> put_text_layout({EmailView, "reset_password.text"})
    |> put_html_layout({EmailView, "reset_password.html"})
    |> render(:reset_password)
    |> Mailer.deliver_later()
  ...
  end

When working with this reset feature I was getting this error:

Request: POST /password-reset
** (exit) an exception was raised:
    ** (Bamboo.View.UndefinedTemplateError) Could not render "reset_password.html" for MyApp.Notifications.Views.EmailView

The following templates were compiled:

* reset.html
* reset.text

        (my_app 0.1.0) lib/my_app/notifications/views/email_view.ex:1: MyApp.Notifications.Views.EmailView.render_template/2
        (bamboo 2.1.0) lib/bamboo/view.ex:139: Bamboo.View.render_within_layout/3
        (bamboo 2.1.0) lib/bamboo/view.ex:98: Bamboo.View.render_html_and_text_emails/2
        (my_app 0.1.0) lib/my_app/notifications.ex:56: MyApp.Notifications.email_password_reset/1
        (my_app 0.1.0) lib/my_app_web/controllers/password_reset_controller.ex:26: MyAppWeb.PasswordResetController.create/2
        (my_app 0.1.0) lib/my_app_web/controllers/password_reset_controller.ex:2: MyAppWeb.PasswordResetController.action/2
        (my_app 0.1.0) lib/my_app_web/controllers/password_reset_controller.ex:2: MyAppWeb.PasswordResetController.phoenix_controller_pipeline/2
        (phoenix 1.5.9) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
        (my_app 0.1.0) lib/plug/error_handler.ex:65: MyAppWeb.Router.call/2
        (my_app 0.1.0) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2
        (my_app 0.1.0) lib/plug/debugger.ex:136: MyAppWeb.Endpoint."call (overridable 3)"/2
        (my_app 0.1.0) lib/my_app_web/endpoint.ex:1: MyAppWeb.Endpoint.call/2
        (phoenix 1.5.9) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.9.0) /Users/adam/development/apps/my_app/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.9.0) /Users/adam/development/apps/my_app/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
        (cowboy 2.9.0) /Users/adam/development/apps/my_app/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
        (stdlib 3.15.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

The reason it is showing reset.html and reset.text is because that was the working configuration before I changed it in the 1) filename, 2) put_html_layout, put_text_layout, and 3) render. So I changed all of these three to reset_password and I got the template not found error. It wasn't compiling until I ran mix clean and mix compile. Then it worked fine.

Not sure if this is an error on my part but thought it would be good to raise this observation.

(Thank you for maintaining this library!)

germsvel commented 3 years ago

Hi @AdamT, thank you for opening this issue. I think that is working as expected (we're compiling the templates), but I'll try to look a little more into it to see if there's something better we can do here.

AdamT commented 1 year ago

Just happened again. I couldn't remember how I solved it, so I managed to find this open issue, and it solved it 😅 Thank you for keeping it open 🙏

If anyone experiences anything similar, please chime in.