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
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.
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.
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.
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…
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With