Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Calling url_for with no arguments fails if default_url_options is not set

#339

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.

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

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. James Cox
  2. Repository
    Repository
    • State changed from new to resolved

    (from [a83ac48501aaba12b3d8abc0c6d6fdf0155de74d]) Fix url_for with no arguments when default_url_options is not explicitly defined. [#339 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    June 17th, 2008 @ 09:02 PM

  3. Repository
    Repository

    (from [7650ff892cbffcaaaf9c5ab72466e98903a202d3]) Fix url_for with no arguments when default_url_options is not explicitly defined. [#339 state:resolved]

    Signed-off-by: Pratik Naik

    http://github.com/rails/rails/co...

    June 17th, 2008 @ 09:02 PM