Update Attribute After Mailing Email - ruby-on-rails

I'm attempting to update an attribute called :lastemailed in my users model after sending an email, but having trouble getting it to work. I've tried putting it in my controller and mailer model but it hasn't worked. Here's the code I'm working with. Thanks for any help!
mailers/page_mailer.rb
class PageMailer < ActionMailer::Base
add_template_helper(PagesHelper)
default from: "mail#example.com"
def reminder_send(user)
#user = user
mail(to: "#{#user.email}", subject: "Your subject")
end
end
pages_controller.rb
def send_reminder_mail
#user = User.find(params[:id])
PageMailer.reminder_send(#user).deliver
flash[:notice] = "Reminder sent!"
redirect_to '/employees'
end
view that calls the mailer
<%= link_to "Email", :controller => "pages", :action => "send_reminder_mail", :id => u.id %>
The mailer works fine, I just can't get the attribute to update. Thank you!

In your Pages Controller, after you send the email and before your redirect, add:
#user.update_attribute(:lastemailed, "New Value")

:lastemailed was a date datatype, and I was trying to save it using Time.now().
Time.now() is datetime datatype, so it needed to be formatted as DateTime.now.to_date in order to update properly.

Related

is it possible to use an instance or variable in the subject of rails mailer and how please :)

I would like to use an instance or variable like below in the mailer subject.
So I would like to get the user name or some other user detail in the subject, I tried several thing but I receive an error every time. I can't find documentation about this either.
mail(:to => "test#gmail, :subject => "current_user.name") or "<%= current_user.name %>"
class PositionMailer < ActionMailer::Base
default from: "test#gmail.com"
layout 'mailer'
def general_message(position, barge_name)
#position = position
#barge_name = barge_name
mail(:to => "1234#gmail.com, :subject => "#{barge_name}")
end
end
controller
def create
#position = Position.new(position_params)
if #position.save!
redirect_to #position
PositionMailer.general_message(#position, #barge_name).deliver
To pass a variable inside quotes you need to do it like this "#{variable.value}
:subject => "#{current_user.name}"
should do it if it has access to current_user
You will have to pass current_user to the mailer along with the position value.
So wherever you are calling that from add current_user, probably looks something like this.
PositionMailer.general_message(position, current_user).deliver

Email Sign Up Confirmation with ActionMailer

Currently I have ActionMailer send an email when a user registers, and I generate a random :sign_in_token with the user.
How can a user then click on the link sent to his email and update the users :registration_complete boolean value to TRUE?
Currently, I am able to send the link and generates a random token, but I don't know how to update the boolean value through the email.
MODELS
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :sign_in_token,
:registration_complete
###This generates my sign_in_token
def generate_sign_in_token
self.sign_in_token = Digest::SHA1.hexdigest([Time.now, rand].join)
end
end
CONTROLLER
def create
#user = RegularUser.new(params[:regular_user])
if #user.save
###Sends the User an email with sign_in_token
UserMailer.registration_confirmation(#user, login_url+"/#{#user.sign_in_token}").deliver
flash[:success] = "Please Check Your Email to Verify your Registration!"
redirect_to (verifyemail_path)
else
render 'new'
end
end
USER_MAILER
def registration_confirmation(user, login_url)
#login_url = login_url
#user = user
mail(:to => "#{user.name} <#{user.email}>", :subject => "Welcome to APP")
end
VIEWS
###Redirects User to Login Page, But how do i connect it to my activate method?
<%= link_to "Complete Registration", #login_url %>
ROUTES
match '/login/:sign_in_token', :to => 'sessions#new'
When they click a link, it takes them to a controller with an action of set_complete using a GET request, which sets the boolean value.
Something like:
def set_complete
user = User.find(params[:user])
user.update_attribute(registration_complete => true)
redirect_to login_path # or whatever your login url is...
end
For the controller action and something like this for the link:
<a href="example.com/registrations/set_complete?user=1" />
Here is a sample of what might go in the routes file:
get "/users/set_complete", :to => "users#set_complete"
You'd probably need to set the user id to whatever you want using erb, andmake a few other app-specific customizations, but this is the general idea.
Hope this helps!

Undefined method for a 'wink' system

I am trying to setup a system where if User A is reading a status from User B and clicks 'I'm interested' it will send a pre default message to User B's inbox (create new record in the Message table). This is sort of like on social networks where users sends a 'wink' to another user. I was able to do a similar setup for another section on the app that works, but I can't get this 'wink' feature to work. After the user clicks 'I'm interested', that link should no longer be available to the user for that status. I get a undefined method 'interested= error.
Intrigued Controller:
def create
#intrigue = current_user.intrigues.build(intrigue_params)
end
def destroy
#intrigue.destroy
end
def repost
#intrigue = Intrigue.find(params[:id]).repost(current_user)
end
#Need help with below code
def interested
#intrigue = User.find(params[:id])
#message = Message.create(:subject => "#{user_id} is Interested in you",
:sender_id => #user_id,
:recipient_id => #intrigue.user_id,
:body => "I saw your date and I'm interested")
#intrigue.message = #message
render :new, alert: 'Your message was sent.'
end
Routes:
resources :intrigues do
member do
post :repost
post :interested
end
end
I added has_one :intrigue to the Message model.
I added belongs_to :message to the Intrigue model.
The intrigues table has the following columns: id, content, user_id
The messages table has the following columns: id, sender_id, recipient_id, subject, body
Remove the line #intrigue.message = #message
Your sender id and recipient id are incorrect also.
def interested
#intrigue = User.find(params[:id])
#intrigue = current_user
#recipient = Intrigue.find(params[:id])
#message = Message.create(:subject => "Someone is Interested in you",
:sender_id => #intrigue.id,
:recipient_id => #recipient.user_id,
:body => "I saw your date and I'm interested")
render :new, alert: 'Your message was sent.'
end
That will have it working with no errors. I would also add a redirect.

How to send mail for multiple models in rails

I am new to rails. I am having problem in mail sending to multiple models. Our project contains parent,teacher and student models.each module having number of users(student,parent,teacher). And also I am having three check box.that is student,teacher,parent.when I click student and teacher.the mail should be sent to all teachers and all students.
If I want send a mail to teacher and also student means ,the problem behind this, mail was sending only to teacher not student. how to solve this problem.and I included my coding.
Controller
def send_news_letter
if params[:announcement].present?
#announcement = Announcement.find(params[:announcement].keys).first
end
if params[:students].present? and params[:teachers].present?
#student = Student.pluck(:email)
#teacher = Teacher.pluck(:email)
UserMailer.send_multiple_email(#student,#teacher,#announcement).deliver
redirect_to announcements_url, :notice => "Newsletter Delivered Successfully" a
end
end
Usermailer.rb
class UserMailer < ActionMailer::Base
default to: Proc.new {Teacher.pluck(:email)},
to: Proc.new {Student.pluck(:email)},
from: "from#example.com"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.password_reset.subject
#
def password_reset(user)
#user = user
mail :to => user.email, :subject => "Password Reset"
end
def send_multiple_email(user,employee,announcement)
#user = user
#employee = employee
#announcement = announcement
mail :subject => "Deliver"
end
end
Please help me.Thanks in advance.
First, in your controller I would store all addresses in one array:
#emails = []
if params[:students].present?
#emails += Student.pluck(:email)
end
if params[:teachers].present?
#emails += Teacher.pluck(:email)
end
if params[:parents].present?
#emails += Parent.pluck(:email)
end
UserMailer.send_multiple_email(#emails,#announcement).deliver
And then in your mailer change to this:
def send_multiple_email(emails,announcement)
#announcement = announcement
emails.each do | address |
mail :to => address, :subject => "Deliver"
end
end
Please note, that if you're referencing your models in the mailer template (such as "Hi <%= #user.name %>!") then you need to load the whole model object. Now you're just using pluck to get a list of all the addresses you want to send to. To get the whole model, change pluck(:email) to all in your controller and change your mailer to reference the attributes in that model instead. This also means your three models need to have the same attribute names (at least the ones you intend to use in the mailer).
Hope it makes sense.

Displaying Action Mailer variable in view returns "nil"

I am trying to let a user submit a form that sends an email message to another user. Everything is working fine, except I am trying to store the body of the email in a variable to display in the view for the mailer and am getting 'nil'. I set up a user_mailer.rb:
class UserMailer < ActionMailer::Base
default :from => "myemail#gmail.com"
def message_user(user, recipient, subject, body)
#user = user
#recipient = recipient
#body = "#{body}"
mail(:to => "#{recipient.name} <#{recipient.email}>", :subject => "Reelify message: #{subject}" )
end
end
and here is my messages_controller.rb :
def new
#user = current_user
if !params[:receiver].blank? and !params[:subject].blank? and !params[:body].blank?
#recipient = User.find_by_name(params[:receiver])
UserMailer.message_user(#user, #recipient, params[:subject], params[:body]).deliver
flash[:notice] = "This flash means it should work.. #{#user.name} #{#recipient.name}"
end
end
When I call #body in the view, it is blank (#body.inspect) results in 'nil'. However, if I do something like: #{body} in place of the subject line, the text will render fine.
Anyone have any clue whats going on?
how about # user, is it displayed correctly? May be #body is used by rails, try rename.

Resources