我有@pitch
accepts_nested_attributes_for
的功能。我希望能够在edit
的编辑页面中add
和features
@pitch
(我正在使用form_for
)。
我已经尝试了很多类似的示例/教程和QA,我的代码似乎是正确的,但我可能为自己创造了一个盲点,因为它不起作用:
显示current features
,并显示new field
。我可以添加new feature
,但是当我尝试edit a current feature
时,它就不会保存到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(简化)
= 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();"}
...
对我缺少什么的想法?
根据要求:
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"}
更新
通过进一步测试,我确实找到了一些有趣的东西:如果我只是更改当前字段,则不会提交任何内容。但是如果在这个动作之后我添加了一个新功能,那么之前的更改也会被提交。
将内联JS移动到外部文件:
基于@max的评论,我在een外部文件中用委托事件处理程序(jQuery)替换了内联javascript。一切仍然有效,但结果相同......这是我的jQuery代码:
// Pitch name is being submitted
$('.edit_pitch').on('change', 'input' || 'select', function( event ) {
$('.edit_pitch').submit();
});