我已经看过一些关于如何使用PHP的新recaptcha的指南,但没有使用Rails.这是我到目前为止的代码:
<script src='https://www.google.com/recaptcha/api.js'></script>
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |f| %>
<%= f.text_field :name %>
<div class="g-recaptcha" data-sitekey="..."></div>
<%= f.submit "Submit" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to users_success_path
else
flash[:notice] = "Failed"
redirect_to new_user_path
end
end
Run Code Online (Sandbox Code Playgroud)
如何验证响应是真还是假?Google关于此主题的文档非常混乱.
现在已经和它斗争了一段时间,不知道为什么它不起作用.
要点是将Devise与LDAP结合使用.除了自定义策略之外,我不需要做任何事情,所以除了自定义策略之外我不需要使用任何东西.
我创建了一个基于https://github.com/plataformatec/devise/wiki/How-To:-Authenticate-via-LDAP,据我所知,一切都应该工作,除非我尝试运行服务器(或rake)路线)我得到了NameError
lib/devise/models.rb:88:in `const_get': uninitialized constant Devise::Models::LdapAuthenticatable (NameError)
Run Code Online (Sandbox Code Playgroud)
我已经将错误追溯到了我的错误 app/models/user.rb
class User < ActiveRecord::Base
devise :ldap_authenticatable, :rememberable, :trackable, :timeoutable
end
Run Code Online (Sandbox Code Playgroud)
如果我删除:ldap_authenticatable然后崩溃消失但我没有路由,user#session并且无法访问登录提示.
我的支持文件:
lib/ldap_authenticatable.rb
require 'net/ldap'
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class LdapAuthenticatable < Authenticatable
def authenticate!
if params[:user]
ldap = Net::LDAP.new
ldap.host = 'redacted'
ldap.port = 389
ldap.auth login, password
if ldap.bind
user = User.where(login: login).first_or_create do |user|
success!(user)
else
fail(:invalid_login)
end
end
end
def login
params[:user][:login]
end
def password
params[:user][:password] …Run Code Online (Sandbox Code Playgroud) 请注意:我没有尝试检测到后退按钮被点击了.我不在乎.
我只是想知道他们的页面是在后面导航中加载的.我想在我的应用程序的某个位置向用户显示警告,如果他们单击后退按钮建议他们刷新.
编辑:
1)我希望我的页面缓存.我们现在生活在一个移动世界.不缓存是一种不好的做法.
2)我希望此功能与URL无关.解耦是一种很好的做法.
我有一个rails 3应用程序,我正在尝试进行测试.我执行以下命令rspec spec/controllers /并得到以下错误:
/config/application.rb:42:in `<<': can't modify frozen array (TypeError)
from c:/Users/#####/documents/#####/config/application.rb:42
Run Code Online (Sandbox Code Playgroud)
它指向我在下面提供的config/application.rb文件.
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module PadOnRails
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically …Run Code Online (Sandbox Code Playgroud) 我目前正在将rails v2中的应用程序迁移到v3
在我的lib/子目录中的一些模块中,例如,我就是lib/search/host_search.rb
用一个
module HostSearch
def do_search(args)
#...
end
end
Run Code Online (Sandbox Code Playgroud)
然后我需要在名为的控制器中使用它 Discovery::HostController < ApplicationController :
def search_results
output = HostSearch.do_search(:search_string => @search_string,
:page => params[:page],
:user => @current_user)
#...
end
Run Code Online (Sandbox Code Playgroud)
但是我得到了:
uninitialized constant Discovery::HostController::HostSearch
Run Code Online (Sandbox Code Playgroud)
..我试着将这些行放在application.rb中,但它不起作用..
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Run Code Online (Sandbox Code Playgroud) 有没有办法告诉Rails某个文件夹中的所有文件都包含在某个命名空间中?
就是
我有一个文件bar.rb在app/foo。Rails将假定此文件已定义Bar,但我希望此文件定义Foo::Bar。
我知道我可以通过将根目录添加到Rails的自动加载路径来实现此目的,但这不是一个真正的解决方案。还有其他方法可以告诉Rails,其中的所有文件都app/foo驻留在Foo名称空间中吗?
编辑:文件树进行澄清
app
assets
controllers
models
foo
bar.rb
quux.rb
Run Code Online (Sandbox Code Playgroud)
我希望能够分别定义Foo::Bar和和中,同时还使用Rails自动加载。无需诉诸如下的树形结构:Foo::Quuxbar.rbquux.rb
app
assets
controllers
models
foo
foo
bar.rb
quux.rb
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Middlewares并练习如何在 Rails 应用程序中安装它。我已经关注了railscast
到目前为止,我已经实施了以下步骤:
1) 创建了一个Rails 4.2名为:Blog的新应用程序
2)在lib文件夹中添加了一个名为response_timer.rb.
class ResponseTimer
def initialize(app)
@app = app
end
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World"]
end
end
Run Code Online (Sandbox Code Playgroud)
3) 添加config.middleware.use "ResponseTimer"到application.rb.
config.middleware.use "ResponseTimer"
Run Code Online (Sandbox Code Playgroud)
但是当我rake middleware在终端中执行命令时,它报告了这个错误:
rake aborted!
NameError: uninitialized constant ResponseTimer
Run Code Online (Sandbox Code Playgroud)
我也尝试添加config.middleware.use "ResponseTimer"的development.rb,但再次面临同样的错误。
我在这里缺少什么?
请帮忙。
我在rails app上下文中有2个问题:
我有一些不是"模型"的课程,但在我的系统中需要,所以我想要分开它们
1)如何在app /?中添加"class"文件夹(如果我创建并放置类,则不包含它们)
2)如何将文件夹"模型"放在"app/class"文件夹中(这里同样的事情,如果我移动它就不包括模型)
谢谢.
我希望创建一个自定义方法(例如def plus_two(x) x + 2 end,让它在应用程序内的任何地方都可以访问——也就是说,可以在控制器、模型、控制台、视图、测试和任何其他.rb文件中访问。我目前在许多领域定义了相同的方法的应用程序,并希望使其干燥
如何做到这一点?
注意:我不介意调用该方法是否需要预先添加某些内容(我已经看到一些答案,其中方法预先带有::名称空间或带有名称空间,但除此之外,我更喜欢在可能的情况下保持代码简洁
我已经阅读了一些类似的问题(例如这个),但我不太明白
ruby ×6
devise ×1
javascript ×1
jquery ×1
recaptcha ×1
routes ×1
rspec ×1
upgrade ×1
validation ×1