简体   繁体   中英

Devise Sendgrid & Cancancan : NameError in UserRegistrations#create

I receive always the same error whenever I try to signup a new user in my Rails app - I use Devise for registrations, CanCanCan and Sendgrid for sending the notification mail at the new user's email.

The new user can signup, but then the error appears:

NameError in UserRegistrations#create

Showing C:/Users/Adsidera/FreshObst/app/views/user_mailer/welcome.html.erb where line #4 raised:

  undefined local variable or method `user' for #<#<Class:0x5777fb8>:0x5bdac78>

Rails.root: C:/Users/Adsidera/FreshObst
Application Trace | Framework Trace | Full Trace

app/views/user_mailer/welcome.html.erb:4:in `_app_views_user_mailer_welcome_html_erb__1004447735_47859732'
app/mailers/user_mailer.rb:14:in `welcome'
app/controllers/user_registrations_controller.rb:7:in `create'

This is the user_registrations_controller.rb

class UserRegistrationsController < Devise::RegistrationsController
before_filter :configure_permitted_parameters

def create
    super
    if @user.persisted?
        UserMailer.welcome(@user).deliver_now
    end
end


protected

# my custom fields are :name, :heard_how
def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) do |u|
    u.permit(:first_name, :last_name,
    :email, :password, :password_confirmation)
end

devise_parameter_sanitizer.for(:account_update) do |u|
    u.permit(:first_name, 
    :email, :password, :password_confirmation, :current_password)
   end
 end
end

And this is the user_mailer.rb

   class UserMailer < ApplicationMailer
default from: "freshobstuser@yahoo.com"

    def contact_form(email, name, message)
        @message = message
        mail(:from => email, 
            :to => 'freshobstuser@yahoo.com',
            :subject => 'A new contact form message from #{name}' 
            )
    end

   def welcome(user)
       @appname = "FreshObst"
        mail(:to => user.email,
         :subject => "Welcome to #{@appname}!")
   end
 end

This is the link to my github as well https://github.com/Adsidera/FreshObst

Thank you! I am getting nuts over it... Anna

UPDATE!

After the advise by Illusionist, the issue seems half-solved, now I have this log:

  UserMailer#welcome: processed outbound mail in 83.0ms

Sent mail to dummy@email.com (638.0ms)
Date: Fri, 04 Mar 2016 13:38:21 +0100
From: freshobstuser@yahoo.com
To: dummy@email.com
Message-ID: <56d981bd691e4_a241d243945917a@Adsidera-PC.mail>
Subject: Welcome to FreshObst!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
 Content-Transfer-Encoding: 7bit
X-SMTPAPI: {"to":[ "mikecontin67@gmail.com" ]}

<html>
  <body>
    <table>
      <tbody>
          <tr>
            <td><h2>Welcome Mike!</h2></td>
           </tr><br>
          <tr><td><p>Thank you for signing up with FreshObst</p></td>
          </tr>
          <tr><td><p>We wish you a fruity and healthy shopping by us ;) </p></td>
          </tr>
           <tr> <td><p><strong>Your FreshObst Team!</strong></p></td>
            </tr><br><br>
            <tr><td><p><small>This is an automated message. Please do not reply to this mail</small></p></td>
           </tr>

       </tbody>
      </table>
   </body>
</html>

 Completed 500 Internal Server Error in 1048ms (ActiveRecord: 86.0ms)

    Net::SMTPFatalError (550 Unauthenticated senders not allowed
   ):
        app/controllers/user_registrations_controller.rb:7:in `create'


 Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (75.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (1.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.0ms)
  Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (2.0ms)
 Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (133.0ms)
 Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (1.0ms)
 Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.0ms)
 Rendered C:/row/Ruby200/lib/ruby/gems/2.0.0/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (224.0ms)

SOLUTION of LAST ERROR : This last error though seemed to be caused by the yahoo.com mail address I chose per default, as from my SendGrid logs: "550 5.7.1 Unauthenticated email from yahoo.com is not accepted due to domain's DMARC policy. Please contact administrator of yahoo.com domain if this was a legitimate mail. Please visit https://support.google.com/mail/answer/2451690 to learn about DMARC initiative."

Changing then the default mailaddress to a non-yahoo mail address solved the issue. Thank you all a lot!

try this

# in app/views/user_mailer/welcome.html.erb
<td><h2>Welcome <%= @user.first_name %>!</h2></td>

and

# in app/mailers/user_mailer.rb
def welcome(user)
  @user = user
  @appname = "FreshObst"
  mail(:to => @user.email,
       :subject => "Welcome to #{@appname}!")
end

SOLUTION TO LAST ERROR : This last error though seemed to be caused by the yahoo.com mail address I chose per default, as from my SendGrid logs: "550 5.7.1 Unauthenticated email from yahoo.com is not accepted due to domain's DMARC policy. Please contact administrator of yahoo.com domain if this was a legitimate mail. Please visit https://support.google.com/mail/answer/2451690 to learn about DMARC initiative."

Changing then the default mailaddress to a non-yahoo mail address solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM