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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

form_for doesn't work wit

#1281

form_for doesn't work with an array of resources. For example, the following doesn't work right now in Rails:


<% form_for [expense.expense_reimbursement, expense] do |form| -%>
 ...
<% end %>

Below is the patch. I do not have the time to submit a proper patch, so in case someone has the time here's the fix and the ticket.


diff --git a/vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb b/vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb
index 7bb82ba..8ca9e68 100644
--- a/vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb
@@ -237,6 +237,13 @@ module ActionView
         case record_or_name_or_array
         when String, Symbol
           object_name = record_or_name_or_array
+          object = nil
+          if args.first.is_a?(Array)
+            object = args.first.last
+          elsif args.first && !args.first.is_a?(Hash)
+            object = args.first
+          end
+          apply_form_for_options!(object, options) if object
         when Array
           object = record_or_name_or_array.last
           object_name = ActionController::RecordIdentifier.singular_class_name(object)

Reported by Zach Dennis · October 27th, 2008 @ 02:33 PM

State: resolved
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to incomplete

    March 10th, 2009 @ 12:32 PM

  2. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from incomplete to stale

    January 20th, 2010 @ 06:16 PM

  3. Rizwan Reza
    Rizwan Reza
    • State changed from stale to invalid

    It's not valid to give an array in form_for.

    January 21st, 2010 @ 04:18 PM

  4. Zach Dennis
    Zach Dennis

    According to its API is most certainly can:

    form_for(record_or_name_or_array, *args, &proc)
    

    Also, it can accept arrays where everything leading up the last element is a symbol:

    <% form_for([:admin, @post]) do |f| %>
       ...
    <% end %>
    

    The suggestion of the this ticket was to make it so you could say:

    <% form_for([@admin, @post]) do |f| %>
       ...
    <% end %>
    

    This made form_for more flexible and didn't require you know in advance the specific type of resource (or its path) in the partial. It could be determined at run-time.

    As something changed recently in Rails which got rid of passing in arrays in form_for?

    January 21st, 2010 @ 06:08 PM

  5. Rizwan Reza
    Rizwan Reza
    • State changed from invalid to open

    Can you make a patch of what you're proposing? Thanks!

    http://rails.lighthouseapp.com/projects/8994/sending-patches

    January 25th, 2010 @ 12:42 AM

  6. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  7. Dan Pickett
    Dan Pickett
    • Tag changed from actionview, form_for to actionview, bugmash, form_for

    May 9th, 2010 @ 06:03 PM

  8. Ryan Bigg
    Ryan Bigg

    This ticket is 2 years old and I don't think it's valid any more.

    May 14th, 2010 @ 12:56 AM

  9. Christopher Redinger
    Christopher Redinger

    Zach,

    Can you provide a little more clarity on what this should do. Specifically, what does @admin provide in the example you cited?

    May 14th, 2010 @ 06:30 PM

  10. Zach Dennis
    Zach Dennis

    Christopher, read my comment on this ticket from January 21st, 2010. Let me know if that is not clear for you.

    Ryan, I think it's still valid. :)

    Everyone... there doesn't seem to be any disagreements with the proposed functionality, just a proper patch. Is this accurate? If so, since no one has taken my offer for making the patch, I will shoot to make one in the next week or so, maybe sooner!

    May 14th, 2010 @ 07:09 PM

  11. Dan Pickett
    Dan Pickett

    I'm +1 on this - if polymorphic_url/path supports an array of objects form_for should as well. If I have time this weekend I'll see if I can craft a patch

    Christopher (and Zach correct me if I'm wrong) I think the intent is for the above example to do a PUT on reimbursement_expense_path(expense.expense_reimbursement, expense)

    May 15th, 2010 @ 02:58 AM

  12. Anil Wadghule
    Anil Wadghule

    This issue looks like non-existent now. Check rails app attached.

    May 15th, 2010 @ 08:37 AM

  13. Rizwan Reza
    Rizwan Reza
    • State changed from open to resolved

    Please let me know if it needs to be reopened.

    May 15th, 2010 @ 10:06 AM