Ok, in my RoR application I have a cinema system where users have preferences, which include saving their favourite actor. What I am trying to do is get it so that when a new film is added, if users have the any of film's actors saved as their favourite they will get an email notifying them of the film. At the minute I am trying to do a simplified version of this where only one user will be emailed if the film has the actor Liam Neeson and the email will be the same one that is received when the user sign's up to the website (I already have this functionality working).
films_controller:
def create
@film = Film.new(film_params)
if @film.save
user = Perference.where(actor: "Liam Neeson").pluck(:user_id).first
if not user.blank?
UserMailer.welcome_email(user).deliver
end
redirect_to @film
else
render 'new'
end
end
user_mailer:
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')
end
end
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>
And 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
But the get the following error:
NoMethodError in FilmsController#create
undefined method `email' for 69:Fixnum
I know that there are problems with my code at the minute, for example it isn't best practice to have cast1, cast2, and cast3 in the films table and there isn't any validation to ensure that users are only emailed if their favourite actor matches the films's actor, but I am just trying to send an email at the minute and will deal with these later.
Can someone please help.
Aucun commentaire:
Enregistrer un commentaire