Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Using form_for() with :as option in routes breaks

#5892

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).

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • 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 %>
    

    October 30th, 2010 @ 08:08 AM

  2. Nick Kezhaya
    Nick Kezhaya

    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).

    October 30th, 2010 @ 06:44 PM