jeudi 2 avril 2015

Ruby on Rails email user when a new record with their preference is added

In my Ruby on Rails application I am creating a cinema site. A user can save their favourite actor in the preferences table, and what I am trying to do is get it so that if a new film is added that has this actor, then all users with that actor as their favourite are emailed the film's details.


I have a email system already working where the user is emailed to confirm that they have successfully registered.


The problem I am having is that I need to retrieve the actors added in a new film record through the controller and if this matches the user's preference then email them. But I am struggling on how to inspect the film's params before saving it.


So far I have done the following in application controller to find user emails where they have the actor Benedict Cumberbatch as their favourite:



def actor_user
actor = "Benedict Cumberbatch"
user = Perference.where(actor: actor).pluck(:user_id)
User.where(id: user).pluck(:email)
end


The problem with this as well is that the actor is hardcoded, whereas what I want to do is extract the names of the film's three actors from the film actors and inspect through these.


Films_controller:



def create
@film = Film.new(film_params)
if @film.save
redirect_to @film
else
render 'new'
end
end


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


application_mailer:



class ApplicationMailer < ActionMailer::Base
#default sender: "ben@thorcinemas.com"
default from: "ben@thorcinemas.com"
layout 'mailer'
end


config/environments/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['my_email
:password => ENV['my_password'],
:authentication => "plain",
:enable_starttls_auto => true
}


Can someone please help. I understand that I will need a new email (as at the minute I only have welcome_email.html.erb), so will need to these in due course.


This is the schema:



create_table "films", force: :cascade do |t|
t.string "title"
t.string "synopsis"
t.string "director"
t.string "cast1"
t.string "cast2"
t.string "cast3"
t.date "release_date"
t.string "warnings"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.string "certificate_id"
t.integer "category_id"
t.integer "hours"
t.integer "minutes"
t.string "video_url"
end

create_table "perferences", force: :cascade do |t|
t.integer "user_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "colour"
t.text "actor"
end

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


I understand that I have broken best practices by having actor1, actor2, actor3, in the films table rather than having an actors table, but for the purpose of this question, please ignore this.


Aucun commentaire:

Enregistrer un commentaire