这是用户创建的列表,Tracks 控制器中的destroy 方法(每个track 引用列表中的每个元素)在单击链接后将其删除。但是,这确实发生了,并且列表保持不变。
控制器:
class TracksController < ApplicationController
def index
@track = Track.new
@tracks = Track.all
end
def create
@track = Track.new(params[:track])
if @track.save
redirect_to :action=>"index"
else
@tracks = Track.all
render :action=>"index"
end
end
def destroy
@track = Track.find(params[:id])
@track.destroy
redirect_to(tracks_url)
end
end
模板(引用创建删除链接并定义其行为的行)
<h1>Playlist</h1>
<%= form_for @track,:url=>{:action=>'create'} do |f| %>
<%= f.error_messages %>
<p>
<%= f.text_field :youtube_url, :required=>true %>
<%= f.submit 'Add' %>
</p>
<% end %>
<hr />
<% if Track.first == nil %>
<p>Database is empty!</p>
<%else%>
<% @tracks.each do |track| %>
<ul class="list_container">
<li class="list_elements"><i class="bullet icon-asterisk"></i> <%= track.youtube_url %></li><i class="icon-remove-circle remove_pos"><%=link_to 'delete', :controller=>"tracks", :method=>:delete,:id=>track.id %></i>
</ul>
<% end %>
<% end %>