2

Since, I am new to rails, so I have want to know a small functionality. I have a reports model in my rails 3 application(not by scaffolding). I am displaying reports one by one through ajax functionality. I want to add a delete link to my each report. I have also created the destroy method in my controller. Now, I don't know how to delete a specific report when I click on the delete link of that particular report. Here's my controller code:-

class ReportsController < ApplicationController
  def index  
    @reports = Report.all(:order => "created_at DESC")  
    respond_to do |format|  
      format.html  
    end  
  end  

  def create  
    @report = Report.create(:description => params[:description])  
    respond_to do |format|  
      if @report.save  
        format.html { redirect_to reports_path }  
        format.js
      else  
        flash[:notice] = "Report failed to save."  
        format.html { redirect_to reports_path }  
      end  
    end  
  end

  def destroy
    @report = Report.find(params[:id])
    if @report.destroy 
      format.html { redirect_to reports_path }  
      format.js         
    end
  end
end

You can assume that my reports are being displayed in the twitter-timeline format and I want to add the delete report feature to each report. Please help me out.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Metal
  • 205
  • 3
  • 11
  • Your controller code looks good. What does your view code look like? If it's "ajax functionality" you probably have to write some javascript that removes the deleted record from the view. – Mischa Mar 29 '11 at 12:57
  • sorry I had the plural reports_path, which is incorrect, it should be report_path as mischa mentioned. – McStretch Mar 29 '11 at 13:30

1 Answers1

2

In your view you'd add a link, button, etc. to send the delete action back to the server.

Using link_to for example:

link_to("Destroy", report_path(report), :method => :delete, :confirm => "Are you sure?")

You can do the same with button_to.

Update:

Sorry I missed the AJAX mention (thanks Jeffrey W.).

You'll also want to add :remote => true if you want to send the delete via AJAX.

McStretch
  • 20,495
  • 2
  • 35
  • 40
  • Since it's a ajax call, make sure you also remove the html corresponding to the entry in the database. If you won't; it will attempt to delete a record which is no longer there. Just saying :-) – Jeffrey W. Mar 29 '11 at 13:02
  • @Jeffrey, thanks for mentioning AJAX, I missed that in the OP's question. I don't think he needs to handle removing the record from the DOM if that's what you mean. I just did this a few days ago and I'm pretty sure the built in rails helpers took care of it for me. – McStretch Mar 29 '11 at 13:10
  • Hi, I accept your answer Mr. McStretch , I tried what you told but the problem now is that, when i click on the delete link, it says Unknown action The action 'show' could not be found for ReportsController. – Metal Mar 29 '11 at 13:17
  • @Metal, take a look at this question for a follow up: http://stackoverflow.com/questions/4446697/why-rails-link-to-does-not-work-for-delete-action. Make sure you have the correct rails.js. – McStretch Mar 29 '11 at 13:24
  • @Metal: Why accept an answer if it doesn't work yet? If you don't accept an answer yet you may get better suggestions. – Mischa Mar 29 '11 at 13:27
  • @McStretch: It should be `report_path(report)`, not `reports_path(report)` – Mischa Mar 29 '11 at 13:28
  • @mischa - I will remember your suggestion. I am sorry . – Metal Mar 29 '11 at 13:40
  • @McStretch Sir, do I need to create a show action in my controller ? I also don't have show.html.erb in my views. – Metal Mar 29 '11 at 13:51
  • @Metal - Did you see my latest update? The path in link_to should be `report_path` rather than `reports_path` assuming that you have :reports listed in your routes.rb as a resource. – McStretch Mar 29 '11 at 13:53
  • @McStretch yes ! i checked that and followed the code you suggested. Now , further I also checked my rails.js , its placed in the public javascripts folder and I also have the :reports listed in my routes.rb. Then, why is this action 'show' error being displayed again & again. – Metal Mar 29 '11 at 13:58
  • @Metal - If you are using jQuery, do you have the correct jQuery rails.js? It's not the same as the default rails.js, which uses Prototype. – McStretch Mar 29 '11 at 14:02
  • @McStretch HI, I found your link useful and successfully implemented the jquery and rails.js and also the delete feature is working now. But I want that my page should automatically refresh after the action delete and display the remnaining reports. Please guide. – Metal Mar 30 '11 at 06:18
  • @Metal - remove the :remote => true for a full refresh. – McStretch Mar 30 '11 at 12:37
  • @McStretch Thanks Sir, I found it useful. – Metal Mar 31 '11 at 05:42