Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render nothing in json?

I'm testing the following method:

  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

The render json nothing line generates the error

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

Changing the line to render json: message: "removed", status: :ok makes no difference. How to render nothing?

Update: I tried the code below, which after deleting responds with No response received, while I would expect the message.

  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
like image 963
Marty Avatar asked Jan 23 '16 23:01

Marty


People also ask

How to render JSON objects lazily?

The code renders the JSON lazily, only building the HTML when the user reveals the JSON by clicking the disclosure icons. This makes it extremely fast to do the initial render of huge JSON objects, since the only thing that renders initially is a single disclosure icon.

How to render JSON for an article using a serializer?

A serializer usually takes one or more arguments in an initialize method and then returns data in a render method. Let’s create one for rendering the JSON for an article. Lucky apps are generated with a BaseSerializer in src/serializers/base_serializer.cr . This serializer has a for_collection method defined that renders a collection of objects.

What is render JSON?

Renderjson is a simple JavaScript package which render JSON into collapsible, themeable HTML. It can be used as debugging tool, however you are the boss and you can use it wherever it is useful.

Does JSON render null string values as null?

1 2 It does not work like this. As described in question, it renders null string values as null in json – Andrus Jan 7, 2014 at 20:32 Add a comment | Your Answer Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid…


1 Answers

If you really want to not render anything:

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

as a parameter, you can add a status code or symbolic status (statuses or just in console Rack::Utils::SYMBOL_TO_STATUS_CODE)

If you want to send a JSON message as response:

render json: { message: "removed" }, status: :ok
like image 69
Oleh Sobchuk Avatar answered Oct 06 '22 00:10

Oleh Sobchuk