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 (, 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 = (, 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| "" 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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.