New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
link_to / form_for doesn't work for singular resource #1769
Comments
|
You're right the error has been around for a long while however a clean solution doesn't readily represent itself. The polymorphic_url helper has no 'intelligence' in how it operates - it has no information about what you've declared as resources in your routes.rb. All it has to go on it the name of the model and how that maps to the named url helpers. In your case when passing a new Company instance it tries to map to the plural path as that's the standard route for the create action. The question is how to determine a singular resource from a regular resource - suggestions are welcome. The best solution I can think of is to try a respond_to? first and if it doesn't then try the singular option - I'll take a look at it. |
|
Ah, didn't know there's already a ticket for htis. So here's my post from the mailing list: 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? |
|
I am wondering why this ticket pointed to 3.2 milestone? |
|
Because it’s not a blocker for the 3.1 release and it may require too many changes to go into a patch release. |
|
i think this bug should be written in rails guides somewhere. for anyone using a singular resource, they will eventually find themselves on this bug page. better to announce first |
|
Still occurring. |
|
for now i just use scope =( |
|
Yep, my first time with the singular resource form, and I walked smack into this bug. Would be great to see a fix in an upcoming version - don't see any target milestone, so I presume it's not under active development at the moment ? |
|
Yep, here too, my first time with singular resource, lost 3h coz i was blaming myself like a first |
|
+1 |
|
I believe respond_with has the same problem. |
|
@felipero yes it will - as will anything where you can pass a model instance and get a url. I haven't forgotten about this issue, it's just trickier than it seems to fix. The problem is there is no easy way to discern whether a model maps to a singular or a regular resource url. Checking for the presence of a collection url doesn't work as the resource may have been specified with The only apparent way I can think of doing it is by creating a data structure that keeps a track of whether a set of routes is a singular resource and then querying that data structure in |
|
Well, it is a non-blocking issue. Thanks for you prompt answer. :) |
|
I think someone ought to patch this. My thinking would not be along the lines of trying routes to fail (and fall back on the plural as in the comments in the rails issue) but go after association reflections. Can we make the assumption that behind every nested resource form there is an activerecord association? No but we can test it for |
|
@zelig firstly, I think anything like that is tying your models to your routes too much. Secondly, what about non-AR models, and finally it doesn't fix the problem for top-level singular resources, e.g; class Basket < ActiveRecord::Base
end
resource :basketThere's nothing in the model that's going to help with that - the only way it to store the fact that it's a singular resource and query that when generating the url. Adding metadata to the model won't help as a model may sometimes be a singular resource (/account) or a multiple resource (/admin/users/1). |
|
Surely this doesn't solve all problems but in the special case of nested AR resources, plurality can be reasonably guessed using model class associations, for others (i.e., top-level resource or non-AR models or models with implicit associations) the plural is a decent guess. This sounds to me a simple improvement but maybe the caveat is that it is not the right solution. So where would you store resource singularity so that polyorphic_url could access this info? In case people are interested in fixing this ugly duckling. |
|
Add me to the list of people who just lost a bunch of time trying to figure out the "right" way to work around this and thinking myself crazy before stumbling upon this obscure reference to a long-standing, known bug. |
And me as well. |
|
To anyone stumbling upon this issue, the workaround is quite simple. Given: resource :billing_infoYou generate the = form_for @billing_info, url: billing_info_path do |f| |
|
@pjg one of the nice features of |
|
Yeah, it does handle both |
|
It would be really nice to have this fixed, or at least to have the problem prominently documented somehow. Just spent 2hrs thinking it was an error on my end! |
|
Wasted 2+ hours hunting this issue down, :( Thanks @pjg for your workaround! |
|
Today, I faced issue too. solved via workaround given Thanks @pjg |
|
I agree with @mikeni that this bug should be documented in the Rails guides. |
|
Thanks @pijg for saving me an hour or three |
|
+1 for this issue! And one more thanks @pjg |
|
Why a bug like this isn't fixed yet? |
|
@philipgiuliani I'm sure you'll discover, judging by the comments above, there isn't any easy way to do this without polluting other classes within Rails for this behaviour. The workaround is quite simple and to be honest, I don't see a problem. Throw it in the docs and just be done with it. Adding |
|
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
A somewhat less invasive redo to rails#23535. The problem that makes rails#1769 so hard to solve, is that `polymorphic_path` is only provided with an database object, so this makes it hard to reinvent the routing tree. This is the best solution I could come up with.
|
See #17066 (comment) |
|
@rafaelfranca Care to explain why you closed this issue? The comment you referenced says it is still open "because it's a difficult problem to solve". Has it finally been fixed? |
|
Oops! Looks like I linked to the wrong comment. I could not find the comment that I explained that it is fine to use the #23138 may fix this issue introducing a new API. |
|
Why it is closed? #23138 is not merged and it is explicitly documented as existing bug in http://guides.rubyonrails.org/v4.1/routing.html |
|
@matkoniecz - it's documented as you said including a workaround with the url param. And there is a bug that may fix this in the longer run. I think that is why it's closed. #23138 will track the progress on the fix now. |
Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g:
resource :basket
direct(class: "Basket") { [:basket] }
This will then generate the following:
>> link_to "Basket", @Basket
=> <a href="/basket">Basket</a>
More importantly it will generate the correct url when used with `form_for`.
Fixes #1769.
Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g:
resource :basket
direct(class: "Basket") { [:basket] }
This will then generate the following:
>> link_to "Basket", @Basket
=> <a href="/basket">Basket</a>
More importantly it will generate the correct url when used with `form_for`.
Fixes #1769.
|
For anyone landing here via google, this is what got implemented: http://edgeguides.rubyonrails.org/routing.html#singular-resources In short, add something like |
|
Couldn't this be fixed in the latest rails implementation by changing the behavior of |
In my routes I have
and in my views
When
new action gets an error when rendering
undefined methodhash_for_companies_path' for #Module:0x00000102bfd3f0`The stack trace reviels that the problem occurs in
lib/action_dispatch/routing/polymorphic_routes.rb:133:inpolymorphic_url'`edit action can render without errors but the forms url is
/company.4where 4 is the id of the company.This seems to be a bug that has been around for a very long time so I think it's time to fix it.
The text was updated successfully, but these errors were encountered: