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

发布于 2024-11-08 23:43:44 字数 1425 浏览 6 评论 0原文

我正在使用 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 %>

可能是什么原因造成的?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-11-15 23:43:44

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

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

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

看看答案

I'm not sure if this is the problem, but I think you want:

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

With Comment.create your model is already saved before your if/else.

Look at the answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文