def index
@cellphones = Cellphone.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @cellphones }
end
SIMPLE ANSWERS PLEASE...
what is format.json used for?
do I have to create a view for it?
how does it relate to javascrip?
how does it relate to to ajax?
please give a simple example of how to use it...
If your Rails application presents an API that utilizes JSON, it can be used with popular Javascript frameworks as well as any other application that can handle JSON. The JSON serialization process consists of two stages: data preparation and transformation to the JSON format. Data preparation consists of transforming Ruby objects into a hash map.
Fast JSON API is a gem you can install in your project. It is one of many gems that you can use. Fast JSON API gives us a new rails generator, serializer, which allows you to quickly create a serializer class. You will need a serializer class for each model, for which you have data you want to serialize.
By using render json:, we are converting all the model instances into JSON. to_json method can add tacked on, but it is optional, as it will be called implicitly, thanks to Rails doing work behind the scenes. Once you have the routes, controller, and models set up, you can start the Rails server to view your appointments.
You may create a view, though, for example, using a JSON builder to customize the output beyond what Rails provides out of the box. JavaScript is often used to make JSON Ajax requests, like for client-side frameworks. Ajax calls often want JSON as a result.
@cellphones
as JSON. You may create a view, though, for example, using a JSON builder to customize the output beyond what Rails provides out of the box./cellphones
.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