以下是scaffold生成的代码
# DELETE /swimming/classtimes/1
# DELETE /swimming/classtimes/1.json
def destroy
@swimming_classtime = Swimming::Classtime.find(params[:id])
@swimming_classtime.destroy
respond_to do |format|
format.html { redirect_to swimming_classtimes_url }
format.json { head :no_content }
end
end
此html代码将发送html格式的请求
<a href="/swimming/classtimes/42" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
或rails code
<%= link_to 'Destroy', swimming_classschedule, :method =>:delete, data: { confirm: 'Are you sure?' }%>
如何告诉动作返回json格式?
答案 0 :(得分:1)
您需要指定生成的<a>
标记标记返回JSON。如果您使用Rails link_to
帮助程序,则可以传递data-type
属性,如下所示:
<%= link_to 'Destroy', swimming_classschedule_path, :method =>:delete, data: { confirm: 'Are you sure?' }, "data-type" => "json" %>
如果您正在寻找纯HTML标记,可以使用以下内容:
<a href="/swimming/classtimes/42" data-confirm="Are you sure?" data-method="delete" rel="nofollow" data-type="json">Destroy</a>
或者,您可以简单地指出href
明确检索JSON:
<a href="/swimming/classtimes/42.json" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
作为一个FYI,第二个代码片段基本上是第一个代码片段的插值。