UrlGenerationError after upgrading to Rails 4.2

9 December, 2015.
Under: tech rails

On upgrading my rails app from 4.0 to 4.2.5 i steeped onto a wierd issue where my form_for tag breaks and starts throwing exception.

A REST model on new action raised an UrlGenerationError exception because of the form_for tag.

For ex: if User is a model my form_for looked like this

    form_for(@user, url: user_path(@user)) do |f|

Raised exception was

    No route matches {:action=>"show", :controller=>"users", :id=>nil} missing required keys: [:id]

The @user object’s id is nil since it’s not yet saved in the database. Previously if it was nil that is been skipped by the the url generation. All these occurrences started throwing errors.

I have to change the form_for tag to

    form_for(@user) do |f|

This posts the form to default users_path.

Nested objects:

    form_for(@user, url: user_address_path(@user, @address)) do |f|

was changed to

    form_for([@user, @address]) do |f|
Share Tweet