0
0
Ruby on Railsframework~5 mins

JSON rendering in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is JSON rendering in Rails?
JSON rendering in Rails means sending data from the server to the client in JSON format, which is easy for web apps to use and understand.
Click to reveal answer
beginner
How do you render JSON in a Rails controller?
Use render json: @object inside a controller action to send the object as JSON to the client.
Click to reveal answer
intermediate
What does render json: @user, only: [:id, :name] do?
It sends only the id and name fields of the @user object as JSON, hiding other data.
Click to reveal answer
beginner
Why use JSON rendering instead of HTML in Rails APIs?
JSON is lightweight and easy for apps and JavaScript to read, making it perfect for APIs that send data, not full web pages.
Click to reveal answer
intermediate
How can you customize JSON output in Rails?
You can use options like only, except, or methods in render json: or create custom serializers.
Click to reveal answer
Which Rails method sends data as JSON to the client?
Aredirect_to @data
Brender json: @data
Csend_data @data
Drender html: @data
How do you exclude fields from JSON output in Rails?
Arender json: @obj, only: [:field1]
Brender json: @obj, hide: [:field4]
Crender json: @obj, except: [:field2]
Drender json: @obj, include: [:field3]
What format does Rails use when you write render json: @user?
AJSON
BHTML
CXML
DPlain text
Which option adds extra methods to JSON output in Rails?
Amethods
Bonly
Cexcept
Dinclude
Why is JSON preferred for APIs over HTML?
AJSON cannot be parsed by JavaScript
BJSON is heavier than HTML
CHTML is not supported by browsers
DJSON is easier for apps to read and use
Explain how to render JSON in a Rails controller and how to control which fields are included.
Think about how you send data and filter fields.
You got /4 concepts.
    Describe why JSON rendering is important for building APIs in Rails.
    Consider how apps use data from servers.
    You got /4 concepts.