Rails Recaptcha 插件正确返回失败,仍然保存模型
class Comment < ActiveRecord::Base
has_ancestry
attr_accessible :name, :content, :post_id, :parent_id
belongs_to :post, :touch => true
end
但是 comments_controller.rb#create 将保存带有或不带有正确验证码的评论。
def create
@comment = Comment.create(params[:comment])
if verify_recaptcha(:model => @comment) && @comment.save
flash[:notice] = "Replied"
redirect_to(post_path(:id => @comment.post))
else
flash[:error] = "Incorrect word verification. Are you sure you\'re human?"
redirect_to(post_path(:id => @comment.post))
end
end
表格如下:
<%= simple_form_for :comment, :url => { :controller => :comments, :action => "create" } do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= raw recaptcha_tags -%>
<%= f.button :submit, "Reply" %>
<% end %>
可能是什么原因造成的?
I am using recaptcha and ancestry with a comment model:
class Comment < ActiveRecord::Base
has_ancestry
attr_accessible :name, :content, :post_id, :parent_id
belongs_to :post, :touch => true
end
But comments_controller.rb#create will save the comment with or without a correct captcha.
def create
@comment = Comment.create(params[:comment])
if verify_recaptcha(:model => @comment) && @comment.save
flash[:notice] = "Replied"
redirect_to(post_path(:id => @comment.post))
else
flash[:error] = "Incorrect word verification. Are you sure you\'re human?"
redirect_to(post_path(:id => @comment.post))
end
end
Here's the form:
<%= simple_form_for :comment, :url => { :controller => :comments, :action => "create" } do |f| %>
<%= f.input :post_id, :required => false, :as => :hidden %>
<%= f.input :parent_id, :required => false, :as => :hidden %>
<%= f.input :name, :label => false, :placeholder => "Name (optional)", :required => false %>
<%= f.input :content, :label => false, :placeholder => "Reply", :as => :text %>
<%= raw recaptcha_tags -%>
<%= f.button :submit, "Reply" %>
<% end %>
What might be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是问题所在,但我认为您想要:
使用
Comment.create
您的模型已经在 if/else 之前保存。看看答案
I'm not sure if this is the problem, but I think you want:
With
Comment.create
your model is already saved before your if/else.Look at the answer here.