0

这是脚手架生成的代码

      # 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>

或导轨代码

<%= link_to 'Destroy', swimming_classschedule, :method =>:delete, data: { confirm: 'Are you sure?' }%>

如何告诉动作返回 json 格式?

4

1 回答 1

2

您需要指定生成的<a>标记标记返回 JSON。如果你使用 Railslink_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>

作为一个仅供参考,第二个代码片段基本上是第一个的插值。

于 2013-06-28T04:29:34.993 回答