The idea is fairly simple.. What it basically does is uses link_to_remote to update the contents of a "div" in the form dynamically with a text for a simple arithmetic equation. The equations is so simple that a 5th grade average student can do them without troubles.. eg. the equation will be something like "8 + 2 = " and then an input text box is provided to enter the value.. Though this technique uses "link_to_remote", it is not too hard to use other ajax methods for the rails as the need be!
Here is a simple code that explains the idea... The code is not perfect and I am not a rails expert, Also it doesn't do any validations than minimum.
Create an application "ajax-test" using rails ajax-test
We are going to add a controller called "main" controller and corresponding views. For this example we don't need any ActiveRecord models.
script/generate controller main
We are going to create the view for this controller and action "index". No layouts..
So create a directory called main inside app/controllers and edit the file
app/views/main/index.rhtml with following contents.
The controller code looks like this.....
There is an additional view defined for submit_post. Which looks like this...
How this works is fairly simple. Basically, whenever the first link is clicked an action in the controller called getequ is called, which generates the equation string for us and stores the value for the equation in the session[:capsum] variable. When the form is subimtted, the value of the text field is checked agains the value stored in the session[:capsum] variable. If the values match, the input is allowed, if they don't match the input is not allowed and the view for submit_test is rendered accordingly..
Since, the value the user has to input is only available upon loading the page and updating div, this value simply will never be known to spam-bot to be used without actually loading the page..
The idea looked so simple to me and pilot to be overlooked for so loong. May be there is a catch which I am not getting yet!! Hopefully someone will point out....
PS: Editing html on blogger is a big pain!! eg. it gets rendered. One way out is to change the "<" of each html tag with "<" and then blogger doesn't try to render it.....