This project is archived and is in readonly mode.
form_for doesn't work with new singleton nested resource
Reported by Jake Paul | January 9th, 2010 @ 07:35 PM
Using a singleton nested resource, such as:
map.resources :users do |users|
users.resource :position
end
form_for doesn't work when the nested resource is being added.
For example, in the controller:
def new
@user = User.find(params[:user_id])
@position = @user.build_position
end
and in the view:
form_for [:user, @member] ...
will throw an exception: undefined method
user_positions_path'
The edit action is identical except the nested resource exists, so in the controller: @position = @user.position instead of @user.build_position. Using form_for [:user, @member] in the edit view creates a form with the correct singular route and put method. It fails to work only when the nested resource is a new record.
I believe the problem is in the polymorphic_url helper, used by apply_form_for_options! in form_for. The helper determines inflection based on new_record? for some reason. If the record is new the inflection is plural. This obviously fails for new singleton nested resources. Here is the pertinent section of polymorphic_url:
inflection =
case
when options[:action].to_s == "new"
args.pop
:singular
when record.respond_to?(:new_record?) && record.new_record?
args.pop
:plural
else
:singular
end
It's possible to work around this using a different form_for syntax to explicitly set the URL, but this seems like a bug because the behavior is inconsistent between the new and edit actions.
Comments and changes to this ticket
-
Jake Paul January 9th, 2010 @ 07:37 PM
Oops, it should say form_for [:user, @position] instead of form_for [:user, @member]. My mistake.
-
Ben Orenstein May 16th, 2010 @ 08:50 PM
I've got a failing test for this in rails3 beta 3. Working on a patch.
-
Rizwan Reza May 19th, 2010 @ 10:19 AM
- no changes were found...
-
Rizwan Reza May 19th, 2010 @ 11:50 AM
- no changes were found...
-
Rizwan Reza May 21st, 2010 @ 12:45 AM
- no changes were found...
-
DHH June 8th, 2010 @ 08:38 PM
- State changed from new to duplicate
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>