--This is the content about the API when implementing asynchronous communication. --The description of the controller and the corresponding .json.jbuilder are in the article.
For requests made asynchronously "Mechanism to prepare a mechanism to respond to the requested data" is realized by creating an API.
Rails defaults to returning HTML, It is also possible to return data in JSON format so that it can be handled from JavaScript.
There are multiple ways to add an API in Rails. This time, how to add it to the controller (action).
Write to return JSON instead of HTML.
Implement for action. Create Songcolor in the following cases. After that, describe in what format the request is returned (response).
<Example A>#####_controller.rb (corresponding controller)
def create
@songcolor = Songcolor.create(color: params[:color], song_id: params[:song_id])
respond_to do |format|
format.json
end
end
▼▼▼ Details ▼▼▼ By using a method called respond_to You can create a response according to the format. It corresponds to the implementation of "make it possible to return in JSON format". In the future, it will be necessary to create a JSON format file "create.json.jbuilder" together with this. Describe the data to be returned. Also, the file name should be the same as the action name to make it correspond. This time it corresponds to the create action, so it will be "create.json.jbuilder".
<Example> Further pick up the important range of the above code
respond_to do |format|
format.json
end
By writing in the action After 〇〇 (action), return it by the method according to the request after respond_to above.
You can also add {} to render to complete it as shown below.
<Example>#####_controller.rb
respond_to do |format|
format.json {
render json: { id: @user.id, name: @user.name }
}
end
ruby:<Example> app/views/messages/show.json.jbuilder
json.content @message.content
=> { content: "@message content" }
jbuilder has a hash like key on the left and value on the right json.content is key, @ message.content is value
In my app
Put the name corresponding to the action in 〇〇 Create 〇〇.json.jbuilder.
Terminal
%app name/views/messages/〇〇.json.jbuilder
The description inside is based on the content described earlier [Reference](https://qiita.com/kusaharajiji/items/3b374c73c12233485feb#jsonjbuilder%E3%81%AE%E3%83%95%E3%82%A1%E3%82 % A4% E3% 83% AB% E3% 82% 92% E4% BD% 9C% E6% 88% 90% E3% 81% 99% E3% 82% 8B% E5% A0% B4% E5% 90% 88 ).
It is loaded when the characters "Rendered" and "jbuilder" are visible.
This is the end! !!
Subsequent asynchronous communication flows also create articles.
The API mechanism is a description of the contents of the controller. After that, information will be exchanged. I'm wondering if jbuilder decides the content of the data and Ajax passes the data. I haven't studied enough about what kind of work is from where to where.
--Description (key and value) when completing with only the controller that does not create the jbuilder file --How to write in jbuilder (also key and value) --The mechanism of HTML and JSON branching in the respond_to method. (I think a request will come, but the flow is unknown)
I wanted to deepen my understanding of this.
It is [here]([ttps: //kayoblog.org/programming_myapp_hidoukitushin/) that describes all the flow of asynchronous communication from the introduction of jQuery.
Recommended Posts