Last active
January 2, 2016 19:28
-
-
Save abe33/8350113 to your computer and use it in GitHub Desktop.
nested_form and strong_parameters issues with 2 nested models forms. The customs gem (https://github.com/inkstak/customs) is used to provide basic resource flow in controllers (hence the `resource_params` method)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-# Here resource is the timeline | |
= simple_nested_form_for resource, url: [resource] do |form| | |
%table.nested_form | |
= form.fields_for :marker_sections, wrapper: false do |section_form| | |
%tr.fields | |
%td | |
%table.nested_form | |
%thead | |
%tr.section | |
%th{colspan: 2}= section_form.input :name | |
= nested_form_remove section_form | |
%tbody | |
= section_form.fields_for :markers, wrapper: false do |marker_form| | |
%tr.fields.marker | |
%td= marker_form.input :name | |
%td= marker_form.input :term_at | |
= nested_form_remove marker_form | |
= nested_form_add section_form, for: :markers, cols: 3 | |
= nested_form_add form, for: :marker_sections | |
= default_actions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Timeline < ActiveRecord::Base | |
has_many :marker_sections | |
has_many :markers, through: :marker_sections | |
accepts_nested_attributes_for :marker_sections, allow_destroy: true | |
end | |
class MarkerSection < ActiveRecord::Base | |
belongs_to :timeline | |
has_many :markers | |
validates :name, presence: true | |
accepts_nested_attributes_for :markers, allow_destroy: true | |
end | |
class Marker < ActiveRecord::Base | |
belongs_to :marker_section | |
validates :name, :term_at, :marker_section, presence: true | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# It gives a 'Unpermitted parameters: 1389351190703, new_marker_sections' in console | |
{"marker_sections_attributes"=>{}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is what the params hash contains prior to sanitization | |
{ | |
"utf8"=>"✓", | |
"authenticity_token"=>"*******", | |
"timeline"=> { | |
"marker_sections_attributes"=>{ | |
"1389351190703"=>{ | |
"name"=>"Foo", | |
"_destroy"=>"false" | |
}, | |
"new_marker_sections"=>{ | |
"markers_attributes"=>{ | |
"1389351191802"=>{ | |
"name"=>"Bar", | |
"term_at(3i)"=>"10", | |
"term_at(2i)"=>"1", | |
"term_at(1i)"=>"2014", | |
"_destroy"=>"false" | |
} | |
} | |
} | |
} | |
}, | |
"id"=>"5" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimelinesController < ApplicationController | |
load_and_authorize_resource | |
def resource_params | |
if params[:timeline].present? | |
timeline_params = params.require(:timeline).permit( | |
:id, | |
marker_sections_attributes: [ | |
:_destroy, | |
:id, | |
:name, | |
:timeline_id, | |
markers_attributes: [ | |
:_destroy, | |
:id, | |
:name, | |
:marker_section_id, | |
:term_at | |
] | |
] | |
) | |
timeline_params | |
else | |
nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Louis, the article's example and my situation is quite different, the questions instances already exist when the form is generated, which mean that they already have an id and an index in the
survey.questions
array.Here's what I discovered about nested_form (as for version 0.3.2):
If I have no sections at all, and if I only create one section and submit the form, the parameters look like:
The record is saved properly.
If I have no sections at all, and I create a section and a marker for that section, the parameters looks like:
Note the
new_marker_sections
parameter, it seems like it hold attributes of nested models of a to create model. But if I try creating two sections with one marker for each, parameters looks like:As you can see here, when we could expect to have one
new_marker_sections
attribute for each new section there's only one for the first marker_sections_attributes entry.In both cases the records aren't saved.
If I already have a section (like after step #1), and I add a marker for this section, the parameters looks like:
Where did come the
null
key? Of course the marker record isn't saved properly. It goes worse if I had more than one marker or if I also create new section at the same time.As it is, I must conclude that nested_form is completely unusable. Sad thing.