This project is archived and is in readonly mode.
fields_for is broken for nested resources since 2.2.x
-
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) endNow, 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) endWhat went wrong here?
-
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.
-
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.
-
DHH
- Assigned user set to Michael Koziarski
-
Michael Koziarski
- Milestone cleared.
-
Michael Koziarski
Is this still broken in current edge rails? fields_for has changed somewhat with the new nested forms code.
-
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
-
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.
-
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.
-
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.
-
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.
-
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.
-
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?
-
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[...]
-
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.