I have a @pitch
that accepts_nested_attributes_for
for features. I want to be able to edit
and add
features
within the edit page of a @pitch
(I'm using form_for
).
I've tried so many similar examples/tutorials and QA's and my code seems to be correct, but I probably created a blind-spot for myself, because it's not working:
The current features
are shown and a new field
is shown. I can add a new feature
, but when I try to edit a current feature
, then it's not saved to the database
.
pitch.rb
class Pitch < ApplicationRecord
...
has_many :features
accepts_nested_attributes_for :features, :allow_destroy => true, reject_if: :all_blank
...
end
pitches_controller.rb
class PitchesController < ApplicationController
before_action :authenticate_user!
...
def edit
@pitch = Pitch.find params[:id]
@pitch.features.build
end
def update
@pitch = Pitch.find params[:id]
if @pitch.update pitch_attributes
redirect_to [:edit, @pitch]
else
render "edit"
end
end
...
private
def pitch_attributes
params.require(:pitch).permit(:name, :summary, :user_id, :pitchtype_ids => [], features_attributes: [:name, :description, :priority, :id])
end
end
edit.html.slim (simplified)
= form_for @pitch do |f|
...
h4
| Step 4/5
= f.fields_for :features do |feature_forms|
.form-group
.input-group
span.input-group-addon
i.glyphicon.glyphicon-th-list
= feature_forms.text_field :name, :placeholder => 'Describe a new feature', :class => 'form-control', :onchange => "this.form.submit();"
#pitch_features.input-group-addon
= feature_forms.select :priority, Feature.priorities.keys, :input_html => {:onchange => "this.form.submit();"}
...
Any thoughts on what I'm missing?
As requested:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"lwXv3PX7uKaHaCpm7jh6YAjqTQNsAgM2h8sy5Hq5vRWw9CQBSguXDO+2+LISCtJ66OT37RbgcmFxAnVK5SBj2A==", "pitch"=>{"name"=>"Remco's pitch blablabla and see what happend", "summary"=>"An add a proper description and something else An add a proper description and something else An add a proper description and something else", "pitchtype_ids"=>["", "1", "2"], "features_attributes"=>{"0"=>{"name"=>"A button to call Remco for getting a beer and some wines", "priority"=>"high", "id"=>"2"}, "1"=>{"name"=>"test2test", "priority"=>"low", "id"=>"23"}, "2"=>{"name"=>"en nog iets anders", "priority"=>"low", "id"=>"25"}, "3"=>{"name"=>"tester van een ander soort", "priority"=>"low", "id"=>"22"}, "4"=>{"name"=>"blaatert", "priority"=>"high", "id"=>"24"}, "5"=>{"name"=>"Alicia", "priority"=>"low", "id"=>"27"}, "6"=>{"name"=>"test 12", "priority"=>"low", "id"=>"26"}, "7"=>{"name"=>"test5", "priority"=>"low", "id"=>"28"}, "8"=>{"name"=>"groepsedingen", "priority"=>"low", "id"=>"29"}, "9"=>{"name"=>"bobbels", "priority"=>"low", "id"=>"30"}, "10"=>{"name"=>"test duizend en nog", "priority"=>"low", "id"=>"32"}, "11"=>{"name"=>"fraat", "priority"=>"low", "id"=>"33"}, "12"=>{"name"=>"nog iets dan toch", "priority"=>"low", "id"=>"34"}, "13"=>{"name"=>"groetenen", "priority"=>"low", "id"=>"35"}, "14"=>{"name"=>"baas", "priority"=>"low", "id"=>"36"}, "15"=>{"name"=>"test zoveel als mogeleijk", "priority"=>"low", "id"=>"31"}, "16"=>{"name"=>"en nu dan? nog iets?", "priority"=>"low", "id"=>"37"}, "17"=>{"name"=>"", "priority"=>"low"}}}, "id"=>"7"}
Update:
By further testing, I did find something interesting: If I just change a current field then nothing is submitted. But if after this action I add a new feature, then the earlier change is also being submitted.
Moved inline JS to external files:
Based on the remarks of @max, I've replaced the inline javascript with a delegated event handler (jQuery) in een external file. Everything is still functional, but with the same result... Here's my jQuery-code:
// Pitch name is being submitted
$('.edit_pitch').on('change', 'input' || 'select', function( event ) {
$('.edit_pitch').submit();
});