-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
121 lines (84 loc) · 3.48 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Recaptchaed
==========
Rails Plugin for adding "recaptcha(recaptcha)":http://www.google.com/recaptcha to a form.
Install
==========
# First, go to "recaptcha(recaptcha)":http://www.google.com/recaptcha and sign up for a api key.
# Next, install the recaptchaed gem:
@gem install recaptchaed@
# Tell Rails to use the gem
Open config/environment.rb and enter the following:
@config.gem "recaptchaed"@
# Run the generator to create config files and to add customizable error messages
@script/generate recaptchaed@
If it runs successfully, you should see the following:
@
create config/recaptchaed.yml
create config/initializers/recaptchaed.rb
added recaptcha i18n error message to config/locales/en.yml
@
# Edit the config/recaptcaed.yml file and add your personal recaptcha public and private api keys
Uninstall
==========
# Run script/destroy recaptchaed
# remove any related custom logic from your views and controllers
Example Usage
=============
This example shows how to protect a comment model with a recaptcha
# Include the recaptcha javascript library in app/views/layouts/comments.html.erb.
@<%=javascript_include_tag 'http://www.google.com/recaptcha/api/js/recaptcha_ajax.js' %>@
# Add the recaptcha markup to the app/views/comments/new.html.erb.
@<h1>New comment</h1>
<% form_for(@comment) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<%= recaptcha_tag (RECAPTCHAED["RECAPTCHA_PUBLIC_KEY"], "recaptcha_div", "blackglass") %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', comments_path %>
@
The "recaptcha site(recaptcha)":http://code.google.com/apis/recaptcha/docs/customization.html has several options for different themes/styles. For example, you can change the theme of the recaptcha to 'white' by passing a different theme string.
@ <%= recaptcha_tag (RECAPTCHAED["RECAPTCHA_PUBLIC_KEY"], "recaptcha_div", "white") %> @
# Add logic to create action inside the comments controller to validate the captcha.
First, include the library in the comments controller:
@
class CommentsController < ApplicationController
include Recaptchaed
...rest of controller...
end
@
Then, protect the create method using the 'validate_captcha(..)' method
@
# POST /comments
# POST /comments.xml
def create
@comment = Comment.new(params[:comment])
@recaptcha = validate_captcha(RECAPTCHAED["RECAPTCHA_PRIVATE_KEY"], request.remote_ip, params['recaptcha_challenge_field'], params['recaptcha_response_field'])
respond_to do |format|
if @recaptcha && @recaptcha['success'] && @comment.save
flash[:notice] = 'Comment was successfully created.'
format.html { redirect_to(@comment) }
format.xml { render :xml => @comment, :status => :created, :location => @comment }
else
if(!@recaptcha['success'])
@comment.errors.add('recaptcha_response_field', :"recaptcha.invalid")
end
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
end
@
When a user fails to enter the correct captcha, the error message that is displayed is defined inside 'config/locales/en.yml'. Feel free to update that to whatever you like. And, it can also be internationalized, if needed.
Enjoy!
http://upgradingdave.com
Copyright (c) 2010 Dave Paroulek, released under the MIT license