0

我觉得我错过了一些小细节,但我可以理解这一点。

我正在尝试为他们在 Rails 4 上的项目连接 simple_captcha ( https://github.com/galetahub/simple-captcha ):

宝石文件:

gem 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git'

application_controller.rb:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include SimpleCaptcha::ControllerHelpers
end

.html.erb:

<%= form_for :test, url: tests_path do |f| %>
...
  <p>
    <%= f.simple_captcha :label => "Enter numbers..", :object => "test" %>
      <%= f.submit %>
  </p>
<% end %>

测试控制器.rb:

class TestsController < ApplicationController
...
def create
    @test = Test.new(test_params)
   if  @test.valid_with_captcha?
     @test.save_with_captcha
     redirect_to root_path
   else
     redirect_to tests_path
    end
  end
end

rails 生成 simple_captcharake db:迁移没有错误。

显示验证码,但不显示自身:是否正确输入验证码无关紧要,仍然是对tests_path的重定向,并且不保留文本。

如果没有正确存储验证码文本,它可以工作:

def create
  @test = Test.new(test_params)
  @test.save
  redirect_to root_path
 end
end
4

1 回答 1

2

事实证明 gem 不支持 rails 4。 Rails 4 的工作 gem:https ://github.com/pludoni/simple-captcha

而且我在代码中误认为应该是这样的:

    def create
    @test = Test.new(captcha_params)

    if @test.save_with_captcha
      redirect_to root_path
    else
      if @test.errors.any?
        redirect_to tests_path
      end
    end
  end

  private
  def captcha_params
    params.require(:test).permit(:id, :text, :captcha, :captcha_key)
  end
于 2014-02-13T05:41:54.667 回答