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.

fields_for is broken for nested resources since 2.2.x

#1808

Until Rails 2.1.x the following code snippet worked for me like a champ:


<% fields_for([@questionnaire, @section, @question_definition, @answer_type]) do |at| %>

All fine and due to the API documentation. BUT, since Rails 2.2.x this doesn't work anymore! Now, I've to do this (the 'longer' version):


<% fields_for(:answer_type, :url => [@questionnaire, @section, @question_definition, @answer_type]) do |at| %>

Reported by q · January 28th, 2009 @ 07:17 PM

State: invalid
Milestone: none
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. q
    q

    As you can see here in the fields_for method of form_helper.rb file the when part for the array case is missing:

    
    def fields_for(record_or_name_or_array, *args, &block)
      raise ArgumentError, "Missing block" unless block_given?
      options = args.extract_options!
    
      case record_or_name_or_array
      when String, Symbol
        object_name = record_or_name_or_array
        object = args.first
      else
        object = record_or_name_or_array
        object_name = ActionController::RecordIdentifier.singular_class_name(object)
      end
    
      builder = options[:builder] || ActionView::Base.default_form_builder
      yield builder.new(object_name, object, self, options, block)
    end
    

    Now, I'm very confused since in github the following code for the same method says:

    
    def fields_for(record_or_name_or_array, *args, &block)
    	raise ArgumentError, "Missing block" unless block_given?
    	options = args.extract_options!
    
    	case record_or_name_or_array
    	when String, Symbol
    	  object_name = record_or_name_or_array
    	  object = args.first
    	else
    	  object = record_or_name_or_array
    	  object_name = ActionController::RecordIdentifier.singular_class_name(object)
    	end
    
    	builder = options[:builder] || ActionView::Base.default_form_builder
    	yield builder.new(object_name, object, self, options, block)
    end
    

    What went wrong here?

    January 28th, 2009 @ 07:35 PM

  2. q
    q

    Finally, I've found the bug: I had both, the github and the rubyforge gem sources (check via gem sources).

    After removing the rubyforge gem source and re-installing rails with all dependencies everything worked fine again.

    January 28th, 2009 @ 09:01 PM

  3. q
    q

    Sorry, but I have to revise myself. The bug is still the. My post from January 28th, 2009 @ 09:01 PM is therefore obsolete.

    January 29th, 2009 @ 11:59 AM

  4. DHH
    DHH
    • Assigned user set to Michael Koziarski

    February 7th, 2009 @ 01:43 PM

  5. Michael Koziarski
  6. Michael Koziarski
    Michael Koziarski

    Is this still broken in current edge rails? fields_for has changed somewhat with the new nested forms code.

    February 7th, 2009 @ 10:09 PM

  7. Michael Koziarski
    Michael Koziarski
    • Assigned user changed from Michael Koziarski to Eloy Duran

    Can you take a look at this eloy? You've rejigged fields_for quite a bit

    February 22nd, 2009 @ 02:32 AM

  8. Eloy Duran
    Eloy Duran

    You say this doesn't work since 2.2.x, so I'm assuming this doesn't have to do with the changes I introduced in 2.3rc1. Or is that in fact the version you are talking about?

    You are probably looking at the wrong fields_for method. There are 2 fields_for methods in form_helpers.rb.

    February 22nd, 2009 @ 04:18 PM

  9. q
    q

    You say this doesn't work since 2.2.x, so I'm assuming this doesn't have to do with the changes I introduced in 2.3rc1.

    That's correct.

    You are probably looking at the wrong fields_for method. There are 2 fields_for methods in form_helpers.rb

    I know both methods. But in fact it doesn't work anymore! The code snippet I gave was taken from the real-world project which worked fine before. I had to rewrite the code as a work-around as mentioned in my first post.

    February 22nd, 2009 @ 04:27 PM

  10. Eloy Duran
    Eloy Duran

    Ok. I still would like to fix it, but am swamped as it is. So if you could pin point the commit which introduced this regression, that would make it much easier to get this fixed. Take a look at using git-bisect for investigating this.

    February 22nd, 2009 @ 04:52 PM

  11. Eloy Duran
    Eloy Duran
    • State changed from new to invalid

    This has actually been deprecated, which seems logical, because the url for the action is only necessary for Form_for, not fields_for.

    See #641 FormHelper#fields_for has illogical (and unused) option.

    February 26th, 2009 @ 12:43 PM

  12. q
    q

    This has actually been deprecated, which seems logical, because the url for the action is only necessary for Form_for, not fields_for.

    But this is the key point! I want to write this:

    
    <% fields_for([@questionnaire, @section, @question_definition, @answer_type]) do |at| %>
    

    but due to the bug it only works when I write this:

    
    <% fields_for(:answer_type, :url => [@questionnaire, @section, @question_definition, @answer_type]) do |at| %>
    

    So, IMHO it's still a bug.

    February 26th, 2009 @ 12:54 PM

  13. Eloy Duran
    Eloy Duran

    You did not answer my question; why would you want to be able to specify the action of a form if fields_for is not meant to be used to create form tags?

    February 26th, 2009 @ 01:01 PM

  14. q
    q

    Sorry, I don't understand your question. I just have nested resources and this @@@<% fields_for([@questionnaire, @section, @question_definition, @answer_type]) do |at| %>

    
    created the required fields for them. I don't know better like doing it that way and btw it is described in the API that way:
    
    

    fields_for(record_or_name_or_array, *args) {|builder.new(object_name, object, self, options, block)| ...} [...]Creates a scope around a specific model object like form_for, but doesn‘t create the form tags themselves[...]

    
    

    February 26th, 2009 @ 01:33 PM

  15. Eloy Duran
    Eloy Duran

    The code examples you give, seem to me as options for building a URL. So for the ‘action’ option of a form tag.

    And that is deprecated behaviour, as you can see on the ticket in which it was removed; #641 FormHelper#fields_for has illogical (and unused) option. If you still feel the need to discuss this further I would suggest you do so on that ticket which is about the deprecation.

    February 26th, 2009 @ 02:02 PM