This project is archived and is in readonly mode.
RoutingError when using form_for, nested resources and :as
-
Piotr Sarnacki
The reason of this error is that form_for does not know about routes. form_for uses polymorphic_url for generating url, which is based on model names. If you have models BlogPost and BlogPostComment, their names are blog_post and blog_post_comment and form_for will simply use it, regardless routes matches that or not.
-
Rasmus Rønn Nielsen
Thank you very much for that quick response.
So I must accept named routes like blog_post_blog_post_comments_path if I want to use form_for? Or is there a "Rails way" of doing this I am not aware of?
-
Santiago Pastorino
- State changed from new to invalid
- Importance changed from to Low
-
Piotr Sarnacki
If you want to use :as option like that, you can set url for form_for manually. I don't know if there is some other option. I will comment here if I find something.
-
David Morton
The reason of this error is that form_for does not know about routes. form_for uses polymorphic_url for generating url, which is based on model names
I see several variations on this, all with a "wontfix" type attitude. I ask, why? It seems that the very name "polymorphic_url" has to do with urls, which are directly related to routes. It just doesn't make any sense to ignore the routes!
-
Piotr Sarnacki
@David: polymorphic_url is dumb in a way that it just maps model name to route:
polymorphic_url(Post.find(1), Comment.find(2)) #=> /posts1/comments/2 polymorphic_url(Post::Comment.find(1)) #=> /post_comments/1It can't analyze the routes and guess if you want to use some specific route that you renamed.