This project is archived and is in readonly mode.

#5892 ✓invalid
Nick Kezhaya

Using form_for() with :as option in routes breaks

Reported by Nick Kezhaya | October 30th, 2010 @ 12:16 AM

This is extremely simple to reproduce.

If, for instance, you create a scaffold:

rails g scaffold Post title:string

and in config/routes.rb, you specify:

resources :posts, :as => 'articles'

then the following method breaks:

form_for(@post)

It attempts to call a method that doesn't exist (posts_path) instead of the proper URL method (articles_path).

Comments and changes to this ticket

  • Andrew White

    Andrew White October 30th, 2010 @ 08:08 AM

    • State changed from “new” to “invalid”
    • Assigned user cleared.
    • Importance changed from “” to “Low”

    Using :as in a resources call doesn't change the name of a model throughout your application. You need to use the url helper in form_for:

    <%= form_for(@post, :url => articles_path) do %>
    <!-- form contents -->
    <% end %>
    
  • Nick Kezhaya

    Nick Kezhaya October 30th, 2010 @ 06:44 PM

    This becomes problematic if you'd want to use a partial (_form.html.erb).

    <%= form_for(@post, :url => articles_path) do %>
    <!-- form contents -->
    <% end %>
    

    This only works if you're trying to create a new Post object. To edit one, however, you'd need to supply article_path(@post).

    This can become particularly messy if you were to do something like: form_for(@post, :url => (action_name == 'edit') ? article_path(@post) : articles_path).

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>

People watching this ticket

Pages