Last active
August 12, 2016 13:15
-
-
Save licatajustin/f348b5a5782bf5d38d35 to your computer and use it in GitHub Desktop.
Rails callbacks for http://stackoverflow.com/questions/31854717/rails-bootstrap-flash-notice-success-is-now-red-and-not-green#31854717
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Contact < ActiveRecord::Base | |
after_create :send_email | |
private | |
def send_email | |
ContactMailer.contact_email(self).deliver | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<p>New Message from Hoot and Holla's Contact form, from <%= "#{@contact.name}, #{@contact.email}" %></p> | |
<p><%= @contact.phone %></p> | |
<p><%= @contact.event %></p> | |
<p><%= @contact.body %></p> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def contact_email(contact) | |
@contact = contact | |
mail(from: @contact.email, subject: 'Contact Form Message').deliver | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create | |
@contact = Contact.new(contact_params) | |
if @contact.save | |
flash[:success] = 'Message Sent.' | |
redirect_to new_contact_path | |
else | |
flash[:danger] = 'Error occurred, messgage not sent.' | |
redirect_to new_contact_path | |
end | |
end |
Actually, apologies for jumping the gun here...i've managed to work it out myself. @contact.event needed to be changed to @contact.event_type and @contact.body needed to be changed to @contacts.comments.
Thanks again Justin for pointing me in the right direction :)
You're welcome @RuNpiXelruN -- sorry for the delay -- no notifications on Gists yet I guess.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Justin, a quick question...again, sorry! I've tidied up my code and files as you've suggested here but am getting a nomethoderror in contacts#create?
Showing /home/action/workspace/hootandholla_lp/app/views/contact_mailer/contact_email.html.erb where line #9 raised:
undefined method `event' for #Contact:0x000000049be368
<%= @contact.event %>
Any ideas as to why??
Sorry to ask again..i really appreciate you taking the time to do all this though!