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
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 ?
That's not syntactically correct ruby - you need to end the respond_to
block before the else
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
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"
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