从创建方法中读取参数
reading parameters from create method
我在 rails 项目上有一个 ruby 正在使用 gem 设计进行身份验证。我正在尝试在注册页面中使用验证码表单来防止机器人创建数千个虚拟登录
我对使用与设计一起使用的 recaptcha gem 不感兴趣,因为我没有正确设置它。
我试着用这个作为参考来设置它https://recaptchainrubyonrails.blogspot.com/
这是我的注册页面
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url:
registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="g-recaptcha" data
sitekey="6LeeL5gUAAAAADbq0vt4d9biAs8oegkPx9ttUkKb"></div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<br>
我的路由文件确实指向一个单独的注册控制器,而不是在设计注册中创建的控制器
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout' }, :controllers => {:registrations => "registrations"}
无论是否单击验证码表单,我都可以创建用户。当我点击它时,我在创建方法中得到参数 g-recaptcha-response,它有一个长文本字符串,例如
"g-recaptcha-response"=>"1234567890abcdefghi..."
我尝试了一种创建方法,该方法会查看该参数并查看它是否不为零。当它为零时,我想后退一页到注册页面。
class RegistrationsController < Devise::RegistrationsController
def create
if not params[:g-recaptcha-response].present?
redirect_to :back
end
end
end
我什至无法获取参数,因为它错误地显示
undefined local variable or method `recaptcha' for #<RegistrationsController:0x7219e5a0> Did you mean? catch"
我正在尝试捕获此参数,查看 g-recaptcha-response 的值,如果响应为 nil 或空字符串,则以某种方式取消用户的创建
使用 'g-recaptcha-response'
而不是 :g-recaptcha-response
。您需要使用字符串语法,因为您不能在符号命名中使用 -
好的,这就是我认为有效的方法。感谢所有帮助过的人
@response = params[:"g-recaptcha-response"]
if @response == nil
flash[:notice] = "Please click on captcha form!"
redirect_back(fallback_location: new_user_registration_path)
else
flash[:notice] = "You signed up successfully!"
redirect_to root_path
end
我在 rails 项目上有一个 ruby 正在使用 gem 设计进行身份验证。我正在尝试在注册页面中使用验证码表单来防止机器人创建数千个虚拟登录
我对使用与设计一起使用的 recaptcha gem 不感兴趣,因为我没有正确设置它。
我试着用这个作为参考来设置它https://recaptchainrubyonrails.blogspot.com/
这是我的注册页面
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url:
registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="g-recaptcha" data
sitekey="6LeeL5gUAAAAADbq0vt4d9biAs8oegkPx9ttUkKb"></div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<br>
我的路由文件确实指向一个单独的注册控制器,而不是在设计注册中创建的控制器
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout' }, :controllers => {:registrations => "registrations"}
无论是否单击验证码表单,我都可以创建用户。当我点击它时,我在创建方法中得到参数 g-recaptcha-response,它有一个长文本字符串,例如
"g-recaptcha-response"=>"1234567890abcdefghi..."
我尝试了一种创建方法,该方法会查看该参数并查看它是否不为零。当它为零时,我想后退一页到注册页面。
class RegistrationsController < Devise::RegistrationsController
def create
if not params[:g-recaptcha-response].present?
redirect_to :back
end
end
end
我什至无法获取参数,因为它错误地显示
undefined local variable or method `recaptcha' for #<RegistrationsController:0x7219e5a0> Did you mean? catch"
我正在尝试捕获此参数,查看 g-recaptcha-response 的值,如果响应为 nil 或空字符串,则以某种方式取消用户的创建
使用 'g-recaptcha-response'
而不是 :g-recaptcha-response
。您需要使用字符串语法,因为您不能在符号命名中使用 -
好的,这就是我认为有效的方法。感谢所有帮助过的人
@response = params[:"g-recaptcha-response"]
if @response == nil
flash[:notice] = "Please click on captcha form!"
redirect_back(fallback_location: new_user_registration_path)
else
flash[:notice] = "You signed up successfully!"
redirect_to root_path
end