Last active
August 29, 2015 13:56
-
-
Save traviskroberts/9201887 to your computer and use it in GitHub Desktop.
Code for blog post "Simple Rails Form Honeypot" - http://nomethoderror.com/blog/2014/02/26/simple-rails-form-honeypot/
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
<%= form_tag form_submit_path do %> | |
<fieldset> | |
<legend>Contact Info</legend> | |
<div class="form-group"> | |
<%= label_tag :name %> | |
<%= text_field_tag :name %> | |
</div> | |
<div class="form-group"> | |
<%= label_tag :email %> | |
<%= text_field_tag :email %> | |
</div> | |
<div class="form-group"> | |
<%= label_tag :phone %> | |
<%= text_field_tag :phone %> | |
</div> | |
<div class="form-group honeypot"> | |
<%= label_tag :content %> | |
<%= text_field_tag :content, nil, tabindex: '-1' %> | |
<span class="help">Do not fill in this field. It is an anti-spam measure.</span> | |
</div> | |
</fieldset> | |
<button type="submit">Submit</button> | |
<%- 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
.form-group { | |
margin-bottom: 1em; | |
width: 100%; | |
&.honeypot { | |
position: absolute; | |
left: -9999px; | |
} | |
} |
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 FormController < ApplicationController | |
def submit | |
SiteMailer.notify(params).deliver unless params[:content].present? # honeypot check | |
flash[:success] = "Your message has been submitted. Thank you!" | |
redirect_to root_url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment