Reputation: 205
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.
Upvotes: 2
Views: 202
Reputation: 20645
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.
Upvotes: 2