Skip to content

Instantly share code, notes, and snippets.

@traviskroberts
Last active August 29, 2015 13:56
Show Gist options
  • Save traviskroberts/9201887 to your computer and use it in GitHub Desktop.
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/
<%= 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 -%>
.form-group {
margin-bottom: 1em;
width: 100%;
&.honeypot {
position: absolute;
left: -9999px;
}
}
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