# Any help with captcha in my comments?

**URL:** <https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848>\
**Category:** Rails\
**Created:** [August 17, 2009, 12:01am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848 "2009-08-17T00:01:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 12:01am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/1 "2009-08-17T00:01:53Z")

</div>

this is my error:

Processing ApplicationController#create (for 127.0.0.1 at 2009-08-17  
00:57:56) [POST]  
Parameters: {“comment”=\>{“name”=\>“asdasd”, “body”=\>“asdasd”},  
“commit”=\>“Add Comment”, “post\_id”=\>“19”,  
“authenticity\_token”=\>“B5dll5fTaDO+ZrEs1S0KkYsmK8VMzCOeDEf731w21zY=”,  
“captcha”=\>“asdasd”, “\_”=\>""}

SyntaxError  
(/Users/webstic/Sites/weblog/app/controllers/comments\_controller.rb:11:  
syntax error, unexpected kELSE, expecting kEND  
else  
^):

this is my code:

class CommentsController \< ApplicationController  
include SimpleCaptcha::ControllerHelpers

def create  
if simple\_captcha\_valid?  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create!(params[:comment])  
respond\_to do |format|  
format.html { redirect\_to @post }  
format.js  
else  
flash[:notice] = “please right down the image verification”  
end  
end  
end  
end

any suggestions ?

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 12:34am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/2 "2009-08-17T00:34:01Z")

</div>

> ```
> end
> 
> ```
> 
> end  
> end
> 
> any suggestions ?

That’s not syntactically correct ruby - you need to end the respond\_to  
block before the else

Fred

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 12:36am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/3 "2009-08-17T00:36:57Z")

</div>

On Aug 16, 2009, at 3:01 PM, Philip G. wrote:

> SyntaxError  
> include SimpleCaptcha::ControllerHelpers
> 
> def create  
> if simple\_captcha\_valid?  
> @post = Post.find(params[:post\_id])  
> @comment = @post.comments.create!(params[:comment])  
> respond\_to do |format|  
> format.html { redirect\_to @post }  
> format.js  
> end # respond\_to

# Move the else out of the respond\_to block. If you redo your

indentation, it will become more obvious.

> ```
> else
> flash[:notice] = "please right down the image verification"
> end # if
> ```

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 3:42am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/4 "2009-08-17T03:42:29Z")

</div>

On Aug 16, 2009, at 3:57 PM, Philip G. wrote:

> > > @comment = @post.comments.create!(params[:comment])
> 
> @comment = @post.comments.create!(params[:comment])
> 
> flash[:notice] = “please right down the image verification”
> 
> any suggestion ?
> 
> thanks for your help guys !

Well, you’re setting the flash, but not telling your method what to  
render, so it’s rendering (by default) create.html.erb, if it exists.  
You should look at your logs to see what the exact error is. The logic  
in your create method really doesn’t handle this right. You should be  
doing the test up front, above the respond\_to block. So, kind of like:

def create  
if simple\_captcha\_valid?  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create!(params[:comment])  
else  
flash[:notice] = “please right down the image verification”  
end

respond\_to do |format|  
format.html { @post.valid? ? redirect\_to @post : render :action  
=\> :new }  
format.js  
end  
end

# create.rjs if you are using prototype. I’m not sure of the exact

syntax here, but

# you get the idea. Something has to send back the js to make the

error appear. Nothing

# will be rendered on success

if flash[:error] # when errors are present  
page.update :flash\_error, flash[:notice]  
end

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 6:57pm UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/5 "2009-08-17T18:57:58Z")

</div>

> def create  
> if simple\_captcha\_valid?  
> @post = Post.find(params[:post\_id])  
> @comment = @post.comments.create!(params[:comment])  
> else  
> flash[:notice] = “please right down the image verification”  
> end
> 
> respond\_to do |format|  
> format.html { @post.valid? ? redirect\_to @post : render :action  
> =\> :new }  
> format.js  
> end  
> end
> 
> # create.rjs if you are using prototype. I’m not sure of the exact
> 
> syntax here, but
> 
> # you get the idea. Something has to send back the js to make the
> 
> error appear. Nothing
> 
> # will be rendered on success
> 
> if flash[:error] # when errors are present  
> page.update :flash\_error, flash[:notice]  
> end  
> ( page.insert\_html :bottom, :comments, :partial =\> @comment  
> page[@comment].visual\_effect :highlight  
> page[:new\_comment].reset )

nop 😕 thats not working 😕  
im not writing correct the rjs code 😕

any help in rjs please ?

---

<div class="post-metadata">

**Author:** ![webstic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/58f4c7/32.png) [@webstic](https://www.ruby-forum.com/u/webstic)\
**Post date:** [August 17, 2009, 12:57am UTC](https://www.ruby-forum.com/t/any-help-with-captcha-in-my-comments/173848/6 "2009-08-17T00:57:32Z")

</div>

Steve R. wrote:

> On Aug 16, 2009, at 3:01 PM, Philip G. wrote:
> 
> > SyntaxError  
> > include SimpleCaptcha::ControllerHelpers
> > 
> > def create  
> > if simple\_captcha\_valid?  
> > @post = Post.find(params[:post\_id])  
> > @comment = @post.comments.create!(params[:comment])  
> > respond\_to do |format|  
> > format.html { redirect\_to @post }  
> > format.js  
> > end # respond\_to
> 
> # Move the else out of the respond\_to block. If you redo your
> 
> indentation, it will become more obvious.
> 
> > ```
> > else
> > flash[:notice] = "please right down the image verification"
> > end # if
> > 
> > ```

i change to this:

class CommentsController \< ApplicationController  
include SimpleCaptcha::ControllerHelpers

def create  
if simple\_captcha\_valid?  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create!(params[:comment])  
respond\_to do |format|  
format.html { redirect\_to @post }  
format.js  
end  
else  
flash[:notice] = “please right down the image verification”  
end  
end  
end

( i post and create js also)

page.insert\_html :bottom, :comments, :partial =\> @comment  
page[@comment].visual\_effect :highlight  
page[:new\_comment].reset

but now if post the comment with right capcha everything is ok  
but if im wrong i have js error on my code (browser popups) before  
flash[:notice] = “please right down the image verification”

any suggestion ?

thanks for your help guys !
