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

时间:2011-05-21 03:15:59

标签: ruby-on-rails ruby-on-rails-3 controller recaptcha simple-form

我正在使用recaptchaancestry评论模型:

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 %>

可能导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:1)

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

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

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

之前

查看答案here