This project is archived and is in readonly mode.

#5798 ✓invalid
Rasmus Rønn Nielsen

RoutingError when using form_for, nested resources and :as

Reported by Rasmus Rønn Nielsen | October 13th, 2010 @ 09:54 AM

I get the following error when trying to use form_for with nested resources, the nested resource route being defined with the :as options:

No route matches {:controller=>"blog_post_comments", :blog_post_id=>#<BlogPost id: 1, title: "asdfasdf" >}

Here is how to replicate this error:
1) Create a new rails3 app
2) Generate two models: BlogPost, BlogPostComment
3) Setup these routes:

resources :blog_posts do
  resources :blog_post_comments, :as => 'comments'
end

4) Add this form_for in views/posts/show.html.erb:

<%= form_for [@blog_post, @blog_post.comments.build] do |f| %>
  <%= f.submit 'Send' %>
<% end %>

5) Use the scaffolded views/controller to create a new post

Now, I expect you to see the same error as shown above. Remove the :as parameter in routes, and the error goes away.

I'm using rails 3-0-edge with ruby 1.9.2.

Comments and changes to this ticket

  • Piotr Sarnacki

    Piotr Sarnacki October 13th, 2010 @ 10:18 AM

    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

    Rasmus Rønn Nielsen October 13th, 2010 @ 10:22 AM

    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

    Santiago Pastorino October 13th, 2010 @ 01:52 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”
  • Piotr Sarnacki

    Piotr Sarnacki October 13th, 2010 @ 01:56 PM

    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

    David Morton November 18th, 2010 @ 04:44 AM

    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

    Piotr Sarnacki November 18th, 2010 @ 05:02 AM

    @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/1
    

    It can't analyze the routes and guess if you want to use some specific route that you renamed.

  • bingbing

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages