Send Ajax requests in rails

Source: Internet
Author: User


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 insideapp/assets/javascripts/application.js//= require jquery_ujs, which includes


    • Allow hyperlinks to:methodsupport non-GET methods with parameters
    • Use hyperlinks, buttons, and forms to:remote => truesupport 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 todata-disable-withtemporarily 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.rb
 
Class CommentsController < ApplicationController
 
   Def create
     Pp params
     @post = Post.where(id: params[:post_id]).first
     @comment = Comment.new(user_id: current_user.id, post_id: params[:post_id], content: params[:comment][:content])
 
     Respond_to do |format|
       If @comment.save
         Format.html # inside is the html code to be generated, which is what I want to add on the page.
         Format.js #executed js code file name and method name are the same, for example, this is create.js.erb
         Format.json {render json: @comment}
       End
     End
   End
 
   Private
     Def comment_params
       Params.require(:comment).permit(:content)
     End
End


Respond_to do |format| "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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.