Recently wrote a blog system practice practiced hand, encountered a problem, the user to add comments to send Ajax requests, but rails in Ajax and Python is not quite the same, Python Ajax is implemented with Js,jquery and rails is not the same, In this record, study for a long time finally figured out a little
Tell the framework that we want to send AJAX requests
In the framework of rails, Ajax is very tightly coupled with the backend.
When we want to send an AJAX request, we can add a property when the template is generated.
Form form, <%= form_for (comment.new), Url:post_comments_path (@post), remote:true do |f|%> When I add remote:true, the AJAX request is sent.
A label is the same <%= link_to 'ajax-request', '/xxxx/create', Remote:true%> so you can send an AJAX request.
Why is it that we can send AJAX requests in such a setting? Because rails uses it internally?
Rails uses a method called unobtrusive JavaScript (UJS) to mount the built-in JavaScript functionality, which you load inside app/assets/javascripts/application.js
//= require jquery_ujs
, which includes
- Allow hyperlinks to
:method
support non-GET methods with parameters
- Use hyperlinks, buttons, and forms to
:remote => true
support Ajax
- hyperlinks, buttons, and forms can be used to jump through the
"data-confirm"
Confirmation dialog window with parameters
- The Send button can be used to
data-disable-with
temporarily close the button when sending out the form to avoid repeated delivery
I also have a problem here. When I send it directly, the server tells me no CSRF token verification
Here we need to add <%= csrf_meta_tags%> in the template so it doesn't go wrong.
The process of the AJAX request
1. When we click to send an AJAX request, we will go to the appropriate controller. For example, I'm here in the Cmment controller.
Comments_conttroller.rbclass Commentscontroller < Applicationcontroller def create pp params @post = Post.where (id:params[:p ost_id]). First @comment = Comment.new (user_id:current_user.id, post_id:params[:p ost_id ], content:params[:comment][:content]) respond_to do |format| If @comment. Save format.html # inside is the HTML code to be generated, which is the format.js I want to add on the page #执行的js代码 file name and method name are identical, For example, this is Create.js.erb Format.json {render json: @comment} end end private def Comment_params Params.require (: comment). Permit (: content) EndEnd
Respond_to do |format| "Https://api.rubyonrails.org/classes/ActionController/MimeResponds.html" can go to the site to see the usage
The main thing is to look back at what we're doing here is the AJAX request, so the return is format.js.
2. In the corresponding Create.js.erb
$ ("#comment_content"). Append ("<%= escape_javascript" (Render ' create ')% >")
<%= Escape_javascript (render ' create ')%>
Back here to find the corresponding template I am here to render ' create ' then he went back to find (because I am here comments, back in the comments directory) _create.html.erb.
So you can send AJAX requests.
Send Ajax requests in rails