RailsCasts Pro episodes are now free!
Learn more or hide this
There is a newer version of this episode, see the revised episode.
<ul> <% for project in @projects %> <li> <%=h project.name %> <%= link_to_destroy "Destroy", project_path(project), confirm_destroy_project_path(project) %> </li> <% end %> </ul>
<% form_for :project, :url => project_path(@project), :html => { :method => :delete } do |f| %> <h2>Are you sure you want to destroy this project?</h2> <p> <%= submit_tag "Destroy" %> or <%= link_to "cancel", projects_path %> </p> <% end %>
map.resources :projects, :member => { :confirm_destroy => :get }
def confirm_destroy @project = Project.find(params[:id]) end
def link_to_destroy(name, url, fallback_url) link_to_function name, "confirm_destroy(this, '#{url}')", :href => fallback_url end
function confirm_destroy(element, action) { if (confirm("Are you sure?")) { var f = document.createElement('form'); f.style.display = 'none'; element.parentNode.appendChild(f); f.method = 'POST'; f.action = action; var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m); f.submit(); } return false; }