2014-02-12 90 views
0

我覺得我錯過了一些小細節,但我可以理解這一點。Rails 4 simple_captcha不工作

我想爲他們的項目連接simple_captcha(https://github.com/galetahub/simple-captcha)on Rails的4:

的Gemfile:

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

tests_controller.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 

軌產生simple_captcha耙分貝:遷移沒有任何錯誤。

驗證碼顯示,但沒有顯示自己:沒有關係正確輸入驗證碼或不是,仍然重定向到tests_path並且文本不被保留。

沒有驗證碼文本存放不當,它的工作原理:

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

回答

2

事實證明創業板不支持軌4.爲Rails 4工作寶石: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