ruby-on-rails - 如何在 json 中不渲染任何内容?

标签 ruby-on-rails json api ruby-on-rails-4

我正在测试以下方法:

  def destroy
    if @article.destroy
      render json nothing: true, message: "removed", status: :ok
    else
      render json: @article, message: "Failed to remove", status: :bad_request
    end
  end
render json nothing行产生错误

undefined method `json' for #Api::V1::ArticlesController:0x000000074f6148



将线路更改为 render json: message: "removed", status: :ok没什么区别。如何不渲染?

更新:我尝试了下面的代码,删除后响应 No response received ,而我期待的消息。
  def destroy
    if @article.destroy
      respond_to do |format|
        format.json { render nothing: true, message: "removed", status: :ok }
      end
    else
      render json: @article, message: "Failed to remove", status: :bad_request
    end
  end

最佳答案

如果你真的不想渲染任何东西:

head :ok # or any another status, e.g. :created, :accepted, etc

作为参数,您可以添加状态代码或符号状态( statuses 或仅在控制台中 Rack::Utils::SYMBOL_TO_STATUS_CODE )

如果您想发送 JSON 消息作为响应:
render json: { message: "removed" }, status: :ok

关于ruby-on-rails - 如何在 json 中不渲染任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970452/

相关文章:

javascript - AngularJS - 将参数传递到 Controller 中?

c# - 使用 Api Id 和 Api Key 进行 RestSharp 身份验证

javascript - Twitter 按钮和 IIFE

ruby-on-rails - Heroku docker 部署

ruby-on-rails - 使用 grouped_collection_select 按枚举对模型进行分组

javascript - 迭代嵌套的 JSON 数组并更改属性的值

json - Postgres : Joining Tables on ID nested within JSON

r - 为什么在 R 中使用 plivo 发送短信失败?

ruby-on-rails - rails : How to make strong parameters easy to read

ruby-on-rails - RoR - 脚手架 - 未定义的方法 'to_sym' for nil :NilClass only an error in the edit method