如何在不重新验证的情况下防止表格提交



我正在使用Rails应用程序上的Google Recaptcha V2验证表格的提交,但是我无法弄清楚如何链接recaptcha验证和表单提交,无论recaptcha是否为检查表格是否总是提交。

我也在使用recaptcha GEM,并根据文档设置所有内容,并且似乎验证本身有效,它只是没有绑定以形成提交。

这是相关代码:

_form.html.erb

<%= form_for([@video, @video.comments.build]) do |f| %>
  <%# Other tags %>
  <%= recaptcha_tags %>
<% end %>

comment_controller.html.erb

def create
    @video = Video.find(params[:video_id])
    @comment = @video.comments.create(comment_params)
    if verify_recaptcha!(model: @comment) && @comment.save
        redirect_to @video
    else
        # I had "render 'new'" here as the documentation did but that caused an error
    end
end

config/initializers/recaptcha.rb

Recaptcha.configure do |config|
  config.site_key  = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  config.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
end

render "new"

生成的错误
Missing template comments/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :arb, :coffee, :jbuilder]}. Searched in: *...

错误似乎是因为没有设置任何注释/新的东西,但我不确定如何添加。

初始化@comment时,您正在调用.create.create制作一个新对象并在一个步骤中保存,因此在验证recaptcha之前将其保存。使用@comment = @video.comments.new(comment_params)

最新更新