Afolabi Olaoluwa Akinwumi Afolabi Olaoluwa Akinwumi - 19 days ago 6
Ruby Question

Rails Mailer messed up

I implemented a RoR mailer with my Ruby on Rails Application and it worked well. But to my surprise, every time a mail is sent, other attributes from my database table is populated in my sent email which I do not want.

To have a diagramatic view of my problem, see this Mailer Preview image:

enter image description here

model(job.rb)

class Job < ActiveRecord::Base
def self.jobs_posted_12hrs_ago
where.not('stripeEmail' => nil).where.not('payola_sale_guid' => nil).where('created_at > ?', Time.now - 12.hours)
end
end


app/mailer/job_notifier.rb

class JobNotifier < ApplicationMailer


def send_post_email(job)
@user = User.where(:email => true).all
emails = @user.collect(&:email).join("#{';'}")
@jobs = job
@job = job
mail(:to => emails, :bcc => User.pluck(:email).uniq, :subject => 'New job posted on FarFlungJobs')
end

end


email template/view(send_post_email.html.erb)

I did iterate over my scope and populated just Comapny Name, Job Title, and Date. but when the email is previewed/sent, other attributes in the database are populated with it.

<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>

<body>
<div>
<h3> Hi, new job just posted on FarFlungJobs.com! </h3>
</div>

<section>
<p>Fresh Job</p>
<hr>
<p>
<% jobs = Job.jobs_posted_12hrs_ago %>
<%= jobs.each do |job| %>
<section class="container">
<div class="row jobs">
<div class="col-md-4" style="font-size: 20px;"><strong><%= job.company_name %></strong></div>
<div class="col-md-4" style="font-size: 20px;"><%= link_to(job.title, job) %></div>
<div class="col-md-4 text-right" style="font-size: 20px; float: left;"><%= job.created_at.strftime('%d %B %y') %></div>
</div>
<%= render 'shared/two_breaks'%>
</section>
<% end %>
</p>

<p>Thanks for subscribing to FarFlungJobs once again. <%= jobs_url %> </p>

</section>
</body>
</html>



What can I do to make sure I only have just the specified attributes in my email template being sent, instead of it add all I dont want?

Answer

I think the problem is this line:

<%= jobs.each do |job| %>

Because of the <%= it will print out the result of the each statement, which is jobs.

Comments