Resource vs resources

There seems to be a small bug with has_one relationships and resource in the routes. Example:

where beta has_one alpha

In routes: resource alpha

In view:

form_for @alpha --> automatic link is alpah_path(id) and not alpha_path and in this case since there is no id in the route, the id takes the place of the format param -> /alpha.1

Thanks! Mike

There have been a lot discussion about this a while ago. However, we couldn’t find the way to determine the correct route to use when you have a singular resource, so it was left unfixable.

The solution you’ll have to do is to use :url

form_for @alpha, :url => alpha_path

What about making the polymorphic routing in rails to try a plural route first. If it fails, try a singleton route. If it fails, raise.

This is not the nicest and most performant way (the result could be

cached in production so it doesn’t really matter in the end) but I guess it should solve most cases. Or is this too much magic?

Corin

I was thinking about the same thing too, but something was blocking me at that moment. Do you mind digging into the source code and see if it’s possible from your perspective, and then send a pull request if you can patch it?

Thanks a lot! :slight_smile:

That's certainly one of the solutions I've thought about and I've got a GitHub issue (#1769) assigned to me regarding this issue so it's on my todo list once 3.1 has shipped.

Andrew

ORLY? I didn’t see that ticket. Lemme have a look!