因为,我是新来的铁轨,所以我想知道一个小功能。 我在我的rails 3应用程序中有一个报告模型(不是通过脚手架)。我通过ajax功能逐个显示报告。我想为每个报告添加一个删除链接。我也在我的控制器中创建了销毁方法。现在,当我点击特定报告的删除链接时,我不知道如何删除特定报告。 这里是我的控制器代码: -如何从模型中删除轨道中的特定帖子?
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
你可以假设我的报告显示在Twitter的时间线格式,我想删除报告功能添加到每个报告。请帮助我。
你的控制器代码看起来不错。你的视图代码是什么样的?如果它是“ajax功能”,则可能必须编写一些JavaScript,从视图中删除已删除的记录。 – Mischa 2011-03-29 12:57:23
对不起,我有多个reports_path,这是不正确的,它应该是report_path作为mischa提到。 – McStretch 2011-03-29 13:30:37