Comments (11)
I can't imagine a use case for this. The primary function is to stop robots from submitting forms. For that, one captcha per page is good enough.
from recaptcha.
Actually I had similar problem: on one page I had two forms (one to send email to friend recommending current page, other to send us opinion about page). Both forms should have been secured from bots.
The problem is recaptcha don't play well in such scenario. The workaround is to display recaptcha just before submitting the form.
My solution
Add recaptcha_tags
wrapper which display recaptcha only if current form is submitted (additionaly check if current form object has any errors - in such case also delay displaying recaptcha).
In application_helper.rb:
def recaptcha_tags(options = {})
form = options.delete(:form)
form_path = form ? form.options[:url] : options.delete(:path)
form_object = form ? form.object : options.delete(:object)
raise 'Form (Form builder object) or path (form path) option for recaptcha_tags is required' unless form || form_path
options[:display] ||= {}
options[:display][:lang] ||= I18n.locale.to_s
this_form_sent = request.method != 'GET' && form_path == request.path
form_without_errors = form_object.nil? || begin
errors = form_object.errors.dup
errors.delete(:base); errors.delete(:recaptcha)
errors.empty?
end
content = if(this_form_sent && form_without_errors)
warning = if form_object && form_object.errors.include?(:recaptcha)
content_tag(:p, content_tag(:strong, form_object.errors[:recaptcha].first), class: 'text-danger')
else ''.html_safe end
super + warning
else ''.html_safe end
content_tag(:div, content, class: 'recaptcha')
end
You have to pass :form
(form builder object) or :path
(form path) options to properly identify current form.
In application_controller.rb:
def verify_recaptcha options={}
if params[:recaptcha_response_field].blank? && options[:model]
attribute = options[:attribute] || :base
options[:model].errors.add attribute, I18n.translate('recaptcha.errors.empty_response') || 'Enter text from image'
false
else
super
end
end
As a extra step you can add this coffescript snippet to remove recaptcha from other forms (if current form has recaptcha, and recaptcha is also present on other form(s)) - as I said recaptcha doesn't play well when put in multiple places on site:
$ ->
$(document).on 'submit', 'form', ->
$('form').not(this).find('.recaptcha').remove()
return true
from recaptcha.
If you use your imagination like @Gee-Bee has, then there are certainly use cases for multiple reCAPTCHA on a page.
Google has docs on how to do this with v2. https://developers.google.com/recaptcha/docs/display So there is a little manual work you need to do because it isn't part of this gem as of yet.
I followed their example and used 'sitekey' : "<%= Recaptcha.configuration.public_key %>",
in the erb for the js.
Maybe @ambethia will integrate the ability to do this at some point. But for now I'm fine doing it mostly manually.
from recaptcha.
Having an example of this I the documentation would be awesome.
—
Jason L Perry
On Mon, Jul 6, 2015 at 5:13 PM, James Carbine [email protected]
wrote:
If you use your imagination like @Gee-Bee has, then there are certainly use cases for multiple reCAPTCHA on a page.
Google has docs on how to do this with v2. https://developers.google.com/recaptcha/docs/display So there is a little manual work you need to do because it isn't part of this gem as of yet.
I followed their example and used'sitekey' : "<%= Recaptcha.configuration.public_key %>",
in the erb for the js.Maybe @ambethia will integrate the ability to do this at some point. But for now I'm fine doing it mostly manually.
Reply to this email directly or view it on GitHub:
#79 (comment)
from recaptcha.
@ambethia I submitted a PR with the example. Since you have not accepted it, is it not what you were wanting/thinking?
from recaptcha.
@bfcoder Your solution may work perfectly. Is there any solution with gem helper functions?
from recaptcha.
@bfcoder I'm showing that PR as merged?
from recaptcha.
@ambethia It was merged, but subsequently removed in #146 Not sure why @grosser decided it was not needed anymore.
from recaptcha.
moved it to the wiki since it's an edge-case usecase that not everybody
needs to read about ... still linked in the readme
https://github.com/ambethia/recaptcha/wiki/Add-multiple-widgets-to-the-same-page
On Tue, Nov 15, 2016 at 6:33 PM, James Carbine [email protected]
wrote:
@ambethia https://github.com/ambethia It was merged, but subsequently
removed in #146 #146 Not sure
why @grosser https://github.com/grosser decided it was not needed
anymore.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#79 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAsZyH7REeg7GOYn-EyAKCr_lw1z8HOks5q-mtqgaJpZM4AYN8K
.
from recaptcha.
ahh, I see. Thanks for clarifying.
from recaptcha.
Awesome, thanks Michael!
On Wed, Nov 16, 2016, 12:05 AM James Carbine [email protected]
wrote:
ahh, I see. Thanks for clarifying.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#79 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAABKgs1fRzulX0AyX3KQtufTZyLiqwEks5q-o8YgaJpZM4AYN8K
.—
Jason L Perry
from recaptcha.
Related Issues (20)
- recaptcha succeeds on view but verify_recaptcha fails in ruby HOT 3
- ActionView::Template::Error: undefined local variable or method `recaptcha_tags' for #<#<Class:0x000055bd2eb3da08>:0x000055bd2f577618> HOT 1
- Enterprise version HOT 5
- Rails 7 / Turbo : g-recaptcha-response is empty HOT 17
- invisible_recaptcha_tags - Redirecting to sessions page upon submission of recaptcha HOT 1
- ReferenceError: grecaptcha is not defined - reCAPTCHA verification failed, please try again. HOT 6
- Invalid API key when using Enterprise HOT 5
- Use www.recaptcha.net when www.google.com is not available? HOT 3
- invisible_recaptcha_tags doesn't work with turbo_streams
- TypeError: Cannot set properties of null at setInputWithRecaptchaResponseTokenForConnectProvider HOT 2
- Remove `json` gem explicit dependency
- No proper documentation/ methods for ENTERPRISE version. HOT 5
- Use parameter `turbo` instead or `turbolinks` HOT 3
- Gem yields 500 Internal Server Error during tests HOT 3
- Timeout related error message is unclear HOT 2
- id on textarea causes duplicate ids if recaptcha_tags is called twice on the same page
- id on textarea causes duplicate ids if recaptcha_tags is called twice on the same page HOT 3
- Contacting Jason Perry
- reCaptcha with v2 fallback not showing up when in production HOT 2
- how do you make the recaptcha visible? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recaptcha.