0

我正在使用带有评论模型的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 %>

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

4

1 回答 1

1

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

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

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

看看这里的答案。

于 2011-05-21T03:27:30.473 回答