I am trying to send an email to a user to confirm that they have successfully registered with the site. I have followed a guide and have the following files.
development.rb:
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => ENV['email'],
:password => ENV['password'],
:authentication => "plain",
:enable_starttls_auto => true
}
application.rb:
gmail_username: 'email'
gmail_password: 'password'
users_controller:
def create
@user = User.new(user_params)
if @user.save
# login is achieved by saving a user's 'id' in a session variable,
# accessible to all pages
session[:user_id] = @user.id
#UserMailer.welcome_email(@user).deliver_now
UserMailer.welcome_email(@user).deliver
@user.add_default_preferences
redirect_to films_path
else
render action: "new"
end
end
mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'no-reply@thorcinemas.com'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
# mail(to: @user.email, subject: 'Welcome to My Awesome Site')
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
mailers/application_mailer:
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout 'mailer'
end
views/welcome_email.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to Thor Cinemas, <%= @user.first_name %>!</h1>
<p>
You have successfully signed up to Thor Cinemas,
your username is: <%= @user.first_name %> <%= @user.last_name %>.<br>
</p>
<p>
To login to the site, just follow this link: <%= @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
But it does not work and when I try to run rails s
I get this output:
C:\Sites\Thor\Under Construction\ThorCinema\new\Lab\Newu\ThorCinema>rails s
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0/lib/rails/ra
iltie.rb:196:in `method_missing': undefined method `gmail_username' for ThorCine
ma::Application:Class (NoMethodError)
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:25:in `<class:Application>'
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:9:in `<module:ThorCinema>'
from C:/Sites/Thor/Under Construction/ThorCinema/new/Lab/Newu/ThorCinema
/config/application.rb:8:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:78:in `block in server'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `tap'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:75:in `server'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.2.0
/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Can someone please help.
Aucun commentaire:
Enregistrer un commentaire