One minute
Use render method in Rails 🚠
You can use the render method to display content in a different format such as JSON or XML. This can be useful when working with APIs.
For example, to render a JSON response, you can add the following code to your controller action:
def show
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @post }
end
end
In this example, if the request is made in HTML format, the show.html.erb
file will be rendered. But if the request is made in JSON format, the @post object will be converted to JSON and returned in the response.
Read other posts
comments powered by Disqus