我有一个@pitch
功能accepts_nested_attributes_for
。我希望能够在(我正在使用)的编辑页面内edit
。add
features
@pitch
form_for
我尝试了很多类似的示例/教程和 QA,我的代码似乎是正确的,但我可能为自己创建了一个盲点,因为它不起作用:
显示current features
和new field
显示。我可以添加一个new feature
,但是当我尝试添加时edit a current feature
,它不会保存到database
.
音高.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();
});