在我的 Rails 应用程序中,我试图创建一个允许用户创建书签的表单。
<% form_tag( contents_path ) do %>
<input name='item_type' value="Bookmark" type="hidden" /></p>
<h3>Create New Bookmark</h3>
<p>Title:<input name='item[title]' type="text" /></p>
<p>URL:<input name='item[url]' type="text" /></p>
<%= submit_tag 'Create' %>
<% end %>
编辑:为清楚起见,这里是上述模板生成的实际 html:
<form action="/contents" method="post"><div style="margin:0;padding:0"><input name="authenticity_token" type="hidden" value="cc709c404365f1a5754a7bf0c3fe79ce9ec9f96b" /></div>
<input name='item_type' value="Bookmark" type="hidden" /></p>
<h3>Create New Bookmark</h3>
<p>Title:<input name='item[title]' type="text" /></p>
<p>URL:<input name='item[url]' type="text" /></p>
<input name="commit" type="submit" value="Create" />
</form>
如您所见,这是一个完全正常的形式。
除非输入实际的 URL,否则它的效果很好,在这种情况下,服务器永远不会响应,并且我在 mongrel.log 文件中收到一条消息
Error reading HTTP body: #<RuntimeError: Socket read return nil>
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:105:in `read_socket'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:77:in `read_body'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/http_request.rb:55:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:149:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:149:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel.rb:268:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:282:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:281:in `each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/configurator.rb:281:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:128:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/../lib/mongrel/command.rb:212:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel_rails:281
/usr/bin/mongrel_rails:16:in `load'
/usr/bin/mongrel_rails:16
可以看出,请求永远不会到达我的控制器代码。
从那以后,我发现提交字段值以字符串“http:/”开头的任何表单都会导致问题。在解决问题之前放置任何其他字符或空格。我也在其他 Rails 应用程序(在同一台服务器上)中尝试过,结果相同。
因此,可以通过在字符串的开头插入额外的空间并在服务器上剥离它来解决该问题。
但我宁愿只在可能的情况下修复服务器端。
我希望这只是服务器上的一个简单错误配置。
我在基于 CPanel 的共享主机安排上在 Rails 2.1.0 下运行。