我有两个班级,Event
和LivingGroup
。 LivingGroup has_many Events
所以我为LivingGroup
的_form.html.erb添加了以下内容:
<div id='event_children'>
<% f.fields_for :events do |event_form| %>
<%= render :partial => 'event', :locals => { :f => event_form } %>
<% end %>
</div>
<p>
<%= add_child_link 'Add Event', 'event', f %>
</p>
包含以下部分_event.html.erb
的多个副本:
<div class="child">
<p>
Event <%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
Event <%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
Event <%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
Event <%= f.label :start_time %><br />
<%= f.datetime_select :start_time %>
</p>
<p>
Event <%= f.label :end_time %><br />
<%= f.datetime_select :end_time %>
</p>
<p>
<%= f.hidden_field :destroy, :class => 'delete' %>
<%= remove_child_link 'remove', f %>
</p>
</div>
但是,我在rails中遇到以下错误:
NoMethodError in Living_groups#new
Showing app/views/living_groups/_event.html.erb where line #24 raised:
undefined method `_destroy' for #<Event:0x7f6da74b1a60>
Extracted source (around line #24):
21: </p>
22: <p>
23: <%= f.hidden_field :destroy, :class => 'delete' %>
24: <%= remove_child_link 'remove', f %>
25: </p>
26: </div>
Trace of template inclusion: app/views/living_groups/_form.html.erb, app/views/living_groups/new.html.erb
但destroy
中定义了events_controller.rb
!
# DELETE /events/1
# DELETE /events/1.xml
def destroy
@event = Event.find(params[:id])
@event.destroy
respond_to do |format|
format.html { redirect_to(events_url) }
format.xml { head :ok }
end
end
编辑:哎呀,should have looked harder。希望这对某人有帮助。