zapico
zapico

Reputation: 2396

Devise not using custom mailer views

I'm using Devise to create/log users in a rails 4 application and I'm having several problems with overwritten views.

I've generated devise views and controllers and they are working correctly, using my new views. To create these views I used:

rails generate devise:views admins

The problem comes when it tries to send a mail, it still uses devise default view:

app/views/devise/mailer/confirmation_instructions.html.erb

The rest of views are correctly been loaded from my /admins/sessions and similar.

I've set config.scoped_views = true in devise.rb too.

Tried to use a custom mailer too and set the template path to my views but it didn't work neither (it still tries to send mail with devise views):

default template_path: 'blablabla/admins/mailer'

Any idea what can be wrong?

Upvotes: 4

Views: 2995

Answers (4)

Gus
Gus

Reputation: 954

I was facing this same issue with latest devise version. In your routes try calling

devise_for :admins

before

devise_for :users

Upvotes: 1

user4713761
user4713761

Reputation:

So this isn't perfect, but it's working for me...

passing opts param for an email version from custom controller

UserNotificationsMailer.confirmation_instructions(user, user.confirmation_token, { subject: "Please confirm your account.", version: "request" } ).deliver_later

then setting an instance variable in the method

def confirmation_instructions(record, token, opts={})
   @version = opts[:version]
   super
end

and using that variable in confirmation_instructions.html.erb

<% if @version.nil? %>
  <p>Welcome <%= @email %>!</p>

  <p>You can confirm your account email through the link below:</p>

  <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
<% elsif @version == "request" %>
  <p><b>From: location.org</b></p>

  <p> <b>Subject:</b> <%= @subject %> </p>

  <p>We noticed you signed up for an account...but have not confirmed yet.</p>
  <p>Confirm you account now and get started!</p>
  <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

  <p> Respond to <%= mail_to(@email) %> </p>
<% elsif @version == "warn" %>
  <p><b>From: location.org</b></p>

  <p> <b>Subject:</b> <%= @subject %> </p>

  <p>We noticed you signed up for an account ... confirmed soon it may be scheduled for removal.</p>
  <p>Confirm you account now and get started!</p>
  <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

  <p> Respond to <%= mail_to(@email) %> </p>
<% end %>

doesn't monkey patch, which is good, and sends different versions. Still searching for a perfect solution, but it works for the moment.

Upvotes: 3

Walakka.jp
Walakka.jp

Reputation: 1

This monkey patch worked for me.

Devise::Mapping.class_eval do
  def self.find_scope!(obj)
    obj = obj.devise_scope if obj.respond_to?(:devise_scope)
    case obj
    when String, Symbol
      return obj.to_sym
    when Class
      Devise.mappings.each_value { |m| return m.name if obj <= m.to }
    else
      # patch
      Devise.mappings.each_value { |m| return m.name if obj.instance_of?(m.to) }
      # Devise.mappings.each_value { |m| return m.name if obj.is_a?(m.to) }
    end

    raise "Could not find a valid mapping for #{obj.inspect}"
  end
end

Rails 4.2.5.1, Devise 3.5.6

Upvotes: 0

Diego Senott
Diego Senott

Reputation: 176

In your config/initializers/devise.rb try to define your template files like this:

  Rails.application.config.to_prepare do
      Devise::SessionsController.layout "admin/registration"
      Devise::RegistrationsController.layout proc { |controller|             user_signed_in? ?
  "admin/application" : "admin/registration" }
      Devise::ConfirmationsController.layout "admin/registration"
      Devise::UnlocksController.layout "admin/registration"
      Devise::PasswordsController.layout "admin/registration"
      Devise::Mailer.layout "mailer/user_email"
    end

The last line is configuring the mailer layout template.

Upvotes: 1

Related Questions