Search code examples
ruby-on-railscsswebrick

Ruby on Rails: odd stylesheet issue


When I open up a page that's using my CSS it will work once, but won't work again until you open up the CSS and re-save it. Every other time I try to directly access my CSS, it works fine. But the other times, it doesn't work and I receive this server output:

[2010-08-01 12:49:37] ERROR NoMethodError: private method `gsub!' called for #<Class:0x7f6d0639ad80>
    /usr/lib/ruby/1.8/webrick/htmlutils.rb:16:in `escape'
    /usr/lib/ruby/1.8/webrick/httpresponse.rb:232:in `set_error'
    /var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:94:in `handle_file'
    /var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:73:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    /usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
    /usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /var/www/rails-blog/vendor/rails/railties/lib/webrick_server.rb:60:in `dispatch'
    /var/www/rails-blog/vendor/rails/railties/lib/commands/servers/webrick.rb:66
    /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    /var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
    /var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
    /var/www/rails-blog/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
    /var/www/rails-blog/vendor/rails/railties/lib/commands/server.rb:49
    /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3
127.0.0.1 - - [01/Aug/2010:12:49:37 BST] "GET /stylesheets/scaffold.css HTTP/1.1" 500 343
- -> /stylesheets/scaffold.css?1280662371

The code I'm using to include the stylesheet is <%= stylesheet_link_tag 'scaffold' %>. I've restarted the Ruby server but with no effect. What could be causing this problem?

This is from reading one of the official guides.

This does not happen when running Mongrel.


Solution

  • This is a bug in WEBrick, see http://www.ruby-forum.com/topic/206225.

    On 10.06.2010 03:19, Michael Pitman (mcp) posted:

    To continue using Webrick, instead of switching to Mongrel, you can also just edit line 15 of lib/ruby/1.8/webrick/htmlutils.rb to read

      str = string ? string.to_s.dup : ""
    

    The problem is that NotModified exception is getting passed to HTMLUtils::escape as a class, rather than a string (originally raised in HTTPServlet::DefaultFileHandler). Since the only things that can really be HTML escaped are strings, it should be safe to always convert the input to string.

    I suspect that the potential performance penalty may prevent a solution like that from making it to the standard ruby library, but it's a simple workaround.

    Alternatively, in the handle_file method of webrick's DispatchServlet (lib/webrick_server.rb in the rails gem), before it calls res.set_error with the exception, you could force the err.message to be a string, or even clear the message, since I think it's discarded anyway for NotModified.

    Michael