rails/rails — pull request #17066 by eval (2014-09-26 13:23:26+00:00)
Title: Let's fix form_for for singular resources

This fixes #1769.

To recap that 36-month, 50+ comment issue:
When you pass an object to `form_for` it will treat the object as a plural resource.
If you pass it an object that should be created/updated as singular resource, it will result in an error or a faulty path, respectively.

Example:
```ruby
# config/routes.rb
resource :account

# create-form
<%= form_for(Account.new) do |f| %>
...

# update-form
<%= form_for(Account.first) do |f| %>
...
```
In both forms we would expect `/account` to be assigned to the action-attribute.

Instead for the create-form you get a `NoMethodError: undefined method "accounts_path"`.
And for the update-form the path `/account.1` is generated (and will likely cause a `ActionController::UnknownFormat`-error on submission). (The reason being that it calls `account_path(account)`, which results in `account` being turned into a format).

Reading from the original issue it causes lots of frustration and wasted time.

While the cause of the error lies somewhere in `polymorphic_path` and would be hard to fix. I think it's `form_for`'s responsibility to not 'leak it's abstraction' in such a trivial case.

This PR takes a stab at fixing it.
It essentially comes down to:
* explicitly set the default format to `nil` for the update-form
* derive the path from the new-path for the create-form

In both cases it still uses `polymorphic_path` to not interfere with generating complex namespaced/nested paths.

Hope it helps!
(If it's useful, I can squash and add a full explanation to the commit message.)