What I want to do:
Send an email to a user confirming that they have successfully registered.
What I have tried to do:
I have followed this guide: http://ift.tt/KXJcBq
And so have ran the command: 'rails generate mailer UserMailer', and have added/edited the following files to look like this:
app/mailers/application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default sender: "ben@thorcinemas.com"
layout 'mailer'
end
app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer
default from: 'notifications@example.com'
def welcome_email(user)
@user = user
@url = 'http://localhost:3000/users/login'
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
app/views/user_mailer/welcome_email.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Welcome to example.com, <%= @user.first_name %></h1>
<p>
You have successfully signed up to example.com,
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>
app/views/user_mailer/welcome_email.txt:
Welcome to example.com, <%= @user.first_name %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.first_name %><%= @user.last_name %>.
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
app/controller/users_controller.rb:
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_later
redirect_to films_path
else
render action: "new"
end
end
Other Details:
Rails version: rails 4.2.0
Operating System: Windows 7
I am connecting to the rails server through localhost:3000 - it is running on my own machine.
I have tried sending emails to a gmail and a tiscali account but neither have worked.
Schema.rb:
create_table "users", force: :cascade do |t|
t.string "password_digest"
t.string "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "house_no"
t.string "street"
t.string "town"
t.string "postcode"
t.string "email"
t.date "date_of_birth"
end
Please note that I'm fairly new to Ruby. Can anyone please help?
Aucun commentaire:
Enregistrer un commentaire