这是我的代码:
观点:
<h1>New Standup</h1>
<%= form_for @standup do |f| %>
Yesterday Items:
<ul>
<%= f.fields_for :yesterday_items do |f1| %>
<li>
<div class="nested-fields">
<%= f1.label :item %>
<%= f1.text_field :item %>
<%= link_to_remove_association 'Remove item', f %>
</div>
</li>
<% end %>
</ul>
<div class="links">
<%= link_to_add_association 'Add Item', f, :yesterday_items %>
</div>
Today Items:
<ul>
<%= f.fields_for :today_items do |f2| %>
<li>
<%= f2.label :item %>
<%= f2.text_field :item %>
</li>
<% end %>
</ul>
<%= f.submit %>
<% end %>
<%= link_to 'Back', standups_path %>
控制器:
class StandupsController < ApplicationController
def index
@standups = Standup.all
end
def new
@standup = Standup.new
@standup.yesterday_items.build
@standup.today_items.build
end
def create
@standup = Standup.new(standup_params)
if @standup.save
redirect_to standups_path
else
render 'new'
end
end
def edit
end
def update
end
def destroy
end
private
def standup_params
params.require(:standup).permit(yesterday_items_attributes: [:id, 1, :item, _:destroy], today_items_attributes: [:id, 1, :item, :_destroy])
end
end
模特:
class Standup < ActiveRecord::Base
has_many :yesterday_items, dependent: :destroy
has_many :today_items, dependent: :destroy
accepts_nested_attributes_for :today_items, :reject_if => :all_blank, :allow_destroy => true
accepts_nested_attributes_for :yesterday_items, :reject_if => :all_blank, :allow_destroy => true
end
当我删除该行时:&lt;%= link_to_add_association'添加项',f,:yesterday_items%&gt;页面呈现与删除assosiation链接,它什么都不做,但至少它现在显示,但添加行时,页面不再呈现。我已经安装并更新了cocoon gem并添加了 // =要求cocoon到aplication.js文件。我不知道问题是什么。
更新:
我写了一个看起来像这样的部分:
<div class="nested-fields">
<%= f.label :item %>
<%= f.text_field :item %>
<%= link_to_remove_association 'Remove item', f %>
</div>
我打电话给:
<%= render "yesterday_form", :f => f1 %>
但是行为没有改变,当添加&lt;%= link_to_add_association'添加项'时,页面仍然不会呈现',f,:yesterday_items%&gt;并且link_to_remove_association仍然没有做任何事情。