This project is archived and is in readonly mode.

#1808 ✓invalid
q

fields_for is broken for nested resources since 2.2.x

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

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| %>

Comments and changes to this ticket

  • q

    q January 28th, 2009 @ 07:35 PM

    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?

  • q

    q January 28th, 2009 @ 09:01 PM

    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.

  • q

    q January 29th, 2009 @ 11:59 AM

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

  • DHH

    DHH February 7th, 2009 @ 01:43 PM

    • Assigned user set to “Michael Koziarski”
  • Michael Koziarski
  • Michael Koziarski

    Michael Koziarski February 7th, 2009 @ 10:09 PM

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

  • Michael Koziarski

    Michael Koziarski February 22nd, 2009 @ 02:32 AM

    • 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

  • Eloy Duran

    Eloy Duran February 22nd, 2009 @ 04:18 PM

    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.

  • q

    q February 22nd, 2009 @ 04:27 PM

    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.

  • Eloy Duran

    Eloy Duran February 22nd, 2009 @ 04:52 PM

    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.

  • Eloy Duran

    Eloy Duran February 26th, 2009 @ 12:43 PM

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

  • q

    q February 26th, 2009 @ 12:54 PM

    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.

  • Eloy Duran

    Eloy Duran February 26th, 2009 @ 01:01 PM

    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?

  • q

    q February 26th, 2009 @ 01:33 PM

    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[...]

    
    
  • Eloy Duran

    Eloy Duran February 26th, 2009 @ 02:02 PM

    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. If you still feel the need to discuss this further I would suggest you do so on that ticket which is about the deprecation.

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>

Pages