Created
March 15, 2019 00:17
-
-
Save Camvillard/a61935539c2eed085ffd0c8b3fdbf5f7 to your computer and use it in GitHub Desktop.
setup a contact form in rails
This file contains hidden or 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
# use contact template from cdlt bisou | |
# setup default from address | |
# add a contact method in MessageMailer | |
def contact(message) | |
@message = message | |
mail(to: 'mail', | |
from: 'mail', | |
subject: @message.subject | |
) | |
end | |
# in MessagesController | |
def new | |
@message = Message.new | |
end | |
def create | |
@message = Message.new(message_params) | |
if @message.valid? | |
MessageMailer.contact(@message).deliver_now | |
redirect_to new_message_path | |
flash[:notice] = "We have received your message and will be in touch soon!" | |
else | |
flash[:notice] = "There was an error sending your message. Please try again." | |
render :new | |
end | |
end | |
private | |
def message_params | |
params.require(:message).permit(:name, :email, :subject, :body) | |
end | |
# testing | |
# (see in test/mailers/previews/message_mailer_preview) | |
def contact | |
message = Message.new(subject: "hello", | |
body: "this is a contact mail test" | |
) | |
MessageMailer.contact(message) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment