开发者

rails can't find destroy method for child form

I have two classes, Event and LivingGroup. LivingGroup has_many Events so I made the following additions to my _form.html.erb for LivingGroup:

<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>

which includes multiple copies of the following partial _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>

However, I get the following error in 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

But destroy is defined in 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

EDIT: Whoops, should have looked harder. Hope this is helpful to someone.


Whoops, as this SO post explains, destroy switched for delete at rails 2.3.5.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜