Rails Recaptcha插件正确返回失败,仍保存模型

Rails Recaptcha plugin correctly returns failure, still saves model

我正在使用带有注释模型的Recaptcha和祖先:

1
2
3
4
5
class Comment < ActiveRecord::Base
  has_ancestry
  attr_accessible :name, :content, :post_id, :parent_id
  belongs_to      :post, :touch => true
end

但是comment_controller.rb#create将保存带有或不带有正确验证码的注释。

1
2
3
4
5
6
7
8
9
10
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

形式如下:

1
2
3
4
5
6
7
8
<%= 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 %>

这可能是什么原因造成的?


我不确定这是否是问题,但我认为您想要:

1
@comment = Comment.new(params[:comment])

使用Comment.create,您的模型已经保存在if / else之前。

在这里查看答案。