This project is archived and is in readonly mode.

#339 ✓resolved
Luke Redpath

Calling url_for with no arguments fails if default_url_options is not set

Reported by Luke Redpath | June 5th, 2008 @ 01:31 PM

Whilst upgrading an app from 1.2 to 2.1, one of my controller tests was failing with:

The error occurred while evaluating nil.[]
method rewrite_url in url_rewriter.rb at line 102
method rewrite in url_rewriter.rb at line 88
method url_for in base.rb at line 621

The reason for this failure is the way the case statement in url_for works:

### ActionController::Base
def url_for(options = nil) #:doc:
  case options || {}
    when String
      options
    when Hash
      @url.rewrite(rewrite_options(options))
    else
      polymorphic_url(options)
  end
end

If options is nil, it will run the case statement against the empty hash and call the "when Hash" branch, but options will still be nil so the rewrite call fails because it expects a hash.

The fix was quite simple (change the default options argument to {} instead of nil) and I've attached the fix as a patch with a test case.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages