【问题标题】:Install recaptcha gem with exsisting devise gem with rubymine 3.1使用 ruby​​mine 3.1 安装现有设计 gem 的 recaptcha gem
【发布时间】:2011-05-28 23:31:13
【问题描述】:

我已经尝试过做的事情:我尝试阅读 github 上的文档并让它在 Rubymine 上运行,我设法将自己与控制器所需的东西以及在配置文件夹。我试过谷歌,发现了一些很好的教程,但是他们缺少的步骤我不一定知道要跳。

我想弄清楚的是:我希望能够在登录注册中使用 recaptcha,利用 devise gem,我已经为我的 devise 登录生成了页面。

到目前为止我所拥有的:

我已经安装并附加了:devise 1.2.rc 和 recaptcha 0.3.1 我在 windows xp 上运行 Rubymine。 Ruby SDK 1.8.7-p302,带有 Rails 3.0.3

我去过谷歌并拥有我的公钥和私钥 下一步告诉我应该将我的密钥添加到 project/config/initializers/recaptcha.rb 这是该文件中包含的内容:

 Recaptcha.configure do |config|
  config.public_key = 'myKey'
  config.private_key = 'myKey'
 end

现在我应该用以下方法修复我的 gemfile

 gem 'recaptcha', :require => 'recaptcha/rails'

我还有我的 config/application.rb 阅读:

 require 'rails/all'
 require 'net/http'

我还在 External Libraries/[gem] devise/app/views/devise/registrations/new.html.erb 中添加了 recaptcha 标记:

   <%= recaptcha_tags %>
   <p><%= f.submit "Sign up" %></p>

我遇到问题的地方(我认为)是 app/controllers/registrations_controller.rbconfig/routes.rb

对于这些文件的具体内容,我有点不知所措。任何帮助将不胜感激,或者有人编写的指导我逐步完成此操作的教程将非常有帮助。谢谢

这是我在菲利克斯发帖后所做的:

外部库/app/controllers/devise/registrations_controller.rb

类设计::注册控制器

      if verify_recaptcha then
          super
      else
          build_resource
          clean_up_passwords(resource)
          flash[:notice] = 'Invalid Captcha'
          render_with_scope :new
      end

  build_resource

  if resource.save
    if resource.active?
      set_flash_message :notice, :signed_up
      sign_in_and_redirect(resource_name, resource)
    else
      set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s
      expire_session_data_after_sign_in!
      redirect_to after_inactive_sign_up_path_for(resource)
    end
  else
    clean_up_passwords(resource)
    render_with_scope :new
  end
end

来自 Project/config/routes.rb:

 devise_for :users, :controllers => {:registrations => 'registrations'}  

这是它吐出的错误:

ActionController::RoutingError(未初始化的常量 RegistrationsController):

在救援/布局 (0.0ms) 内渲染 C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb .... 。 有任何想法吗?

【问题讨论】:

    标签: ruby-on-rails-3


    【解决方案1】:

    对于您的路线,除了指定您的自定义控制器之外,您可以保留正常的设计路线:

    devise_for :users, :controllers => {:registrations => 'registrations'}
    

    在registrations_controller.rb 中,您希望继承Devise RegistrationsController 并覆盖“create”方法:

    class RegistrationsController < Devise::RegistrationsController
        def create
            if verify_recaptcha then
                super
            else
                build_resource
                clean_up_passwords(resource)
                flash[:notice] = 'Invalid Captcha'
                render_with_scope :new
            end
        end
    end
    

    【讨论】:

    • 所以在我的 registrations_controller.rb 我有我当前的 def create...我要完全替换还是添加?
    • 好吧,您当前的注册控制器应该是设计注册控制器的子类,否则为空,除非您进行了其他自定义。如果您已经定义了 create 方法,您只想在 verify_recaptcha 返回 false 时停止注册过程。
    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多