奇怪的RoR问题; googlebot 收到错误,人类看到页面
我维护一个 Ruby-on-Rails 网站(实际上运行 JRuby 1.5.5,Rails 版本 2.3.10),并且看到一些相当奇怪的东西。当搜索引擎机器人访问时,某个控制器操作导致日志中出现异常:
ActionView::TemplateError (can't convert nil into String) on line #14 of app/vie
ws/scenarios/show_send_message.rhtml:
11: <% if ! is_logged_in? %>
12: <p>Your email (optional, used to contact you if necessary):<br /><%= tex
t_field_tag 'user_email', @user_email || '', :size => 50 %>
13: <% if ! is_human? %>
14: <%= show_simple_captcha %>
15: <% end %>
16: <% end %>
17: <p><%= submit_tag 'Send' %>
...但是,当我
有谁知道为什么 RoR 对机器人的响应与对真实浏览器的响应不同?我并不是在寻找问题的答案,因为我是一种继续调试的方法,但我很乐意接受前者。
I maintain a Ruby-on-Rails website (actually running JRuby 1.5.5, Rails version 2.3.10) and am seeing something fairly strange. A certain controller action is causing an exception in the logs, when accessed by a search engine bot:
ActionView::TemplateError (can't convert nil into String) on line #14 of app/vie
ws/scenarios/show_send_message.rhtml:
11: <% if ! is_logged_in? %>
12: <p>Your email (optional, used to contact you if necessary):<br /><%= tex
t_field_tag 'user_email', @user_email || '', :size => 50 %>
13: <% if ! is_human? %>
14: <%= show_simple_captcha %>
15: <% end %>
16: <% end %>
17: <p><%= submit_tag 'Send' %>
... However, when I view the page as a human, it works fine. "show_simple_captcha" is a method from a plugin, and it can't return nil.
Does anyone have any idea why RoR would respond differently to a bot than it would to a real browser? I'm not so much looking for the answer to the problem as I am a way to proceed with debugging, but I'll gladly accept the former.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我尝试查看该页面时,我也得到了 500。您的代码很可能有问题。例如,您希望设置会话或 cookie 变量,但实际上并未设置。
When I tried to view the page, I got 500 too. There's a big chance something is wrong with your code. For example, you expect session or cookie variable to be set, when it's not.
从上面的错误消息和代码片段中很难看出,但此操作是否有可能假设您已登录该网站?
我的第一个想法是应用程序具有某种类型的状态(cookie、会话等),并且当您访问网站时,您已经使用应用程序建立了状态,因此您不会遇到任何问题。另一方面,Google Bot 可能只是直接跳转到此页面,并且缺少您的网站可能假设已设置的任何状态。
我想到的另一件事是验证码控件可以根据用户代理做出一些渲染决策。也许您的验证码控件不喜欢 Google Bot 使用的用户代理。尝试获取那些可让您更改用户代理的 Firefox 插件之一,并将其设置为您在日志中看到的值。点击您的网站,看看会发生什么。
It is hard to tell from the above error message and code snippet, but is it possible that this action is making the assumption that you are logged in to the site?
My first thought is that the application has some type of a state (cookie, session, etc) and that when you are visiting the website you already have established your state with the app and therefore you do not experience any problems. The Google Bot on the other hand, may just be jumping right to this page and will lack any state your site might assume was already setup.
Another thing that pops to my mind, is the captcha control could be making some rendering decisions based on user agent. Perhaps the user agent the Google Bot is using isn't liked by your captcha control. Try getting one of those Firefox plugins that lets you change your user agent, and set it to the value you see in your logs. Hit your website and see what happens.
如果您可以模拟机器人,请开始调试插件。我的猜测是,这是插件本身的错误,而不是您的应用程序的错误。
If you can emulate a bot, start debugging the plugin. My guess is that it's a bug with the plugin itself and not your app.
我也面临着同样的问题。在研究该插件时,我可以理解,在渲染有验证码的页面时,会调用“simple_captcha_controller”来渲染验证码图像(查看验证码图像的“src”属性)。在此 url 中,还传递了参数“simple_captcha_key”。计算密钥并将其作为参数传递给 simple_captcha_controller'。键值对存储在表“simple_captcha_date”中。因此,当人们查看表单时,密钥是预先计算的(并且有一个值)并发送到“simple_captcha_controller”,因此我们没有看到问题。但是,当机器人“查看”时(我不太了解其机制),传递给“simple_captcha_controller”的密钥在“simple_captcha_data”表中没有值并返回 nil。 (simple_captcha_config.rb 中的“SimpleCaptchaData.get_data(key).value”返回 nil)
如果传递任意密钥,则可能会重现该错误。
I too am facing the same problem. On investigating the plugin, I could understand that while rendering the page on which there's captcha, the 'simple_captcha_controller' is called for rendering the captcha-image (look at the 'src' attribute of the captcha image). In this url, the param 'simple_captcha_key' is also passed. The key is calculated and is passed as a param to simple_captcha_controller'. The key-value pair is stored in the table 'simple_captcha_date'. So, when a human is viewing the form, the key is pre-calculated(and has a value) and sent to the 'simple_captcha_controller' and hence we don't see the problem. However, when a bot is 'viewing', the mechanism of which I don't quite understand, the key passed to the 'simple_captcha_controller' doesn't have a value in the 'simple_captcha_data' table and returns nil. ('SimpleCaptchaData.get_data(key).value' in simple_captcha_config.rb returns nil)
The error can be reproduced if arbitrary keys are passed.