问题
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...
回答1:
- It's used to return JSON.
- No, it renders
@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. - JavaScript is often used to make JSON Ajax requests, like for client-side frameworks.
- Ajax calls often want JSON as a result. They may also request HTML to render directly.
- That is an example of how to use it; make an Ajax request to
/cellphones
.
来源:https://stackoverflow.com/questions/14330110/how-to-use-rails-format-json