Skip to content


ActionView::TestCase and nil.url_for

If you’re having problems testing your helpers because of messages like these:

The error occurred while evaluating nil.url_for

The problem, it turns out is ActionView::TestCase doesn’t like url_for, it seems to expect you’re using named routes. So, in your helper, instead of doing something like:

def link_to_awesomeness(my_object)
  if my_object.awesome?
    link_to 'Awesomeness', :action => 'show_awesomeness', :id => my_object
  else
    link_to 'Make Awesome', :action => 'make_awesome', :id => my_object
  end
end

Try something more like this:

def link_to_awesomeness(my_object)
  if my_object.awesome?
    link_to 'Awesomeness', awesomeness_path(my_object)
  else
    link_to 'Make Awesome', make_awesome_path(my_object)
  end
end

Of course this implies you’ve created those named routes in your config/routes.rb, e.g.:

map.awesomeness 'my_objects/:id/awesomeness', :controller => 'my_objects', :action => 'show_awesomeness'
map.make_awesome 'my_objects/:id/make_awesome', :controller => 'my_objects', :action => 'make_awesome'

Posted in Rails, Ruby.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.