找到你要的答案

Q:Purpose of remote: true in rails

Q:远程目的:真正的Rails

I followed a rails tutorial where we submitted a form with ajax. We used remote: true in form_for:

<%= form_for @task, remote: true do |f|   %>

Everything works fine. But I'm confused about remote: true. What is the purpose of remote: true?

我跟着一个Rails教程我们提交表单Ajax。我们使用远程:真正的form_for:

<%= form_for @task, remote: true do |f|   %>

一切都好。但我对远程感到困惑:真实。远程的目的是什么:真的吗?

answer1: 回答1:

JS

remote: true is a part of the Rails UJS (unobtrusive Javascript) driver. It just adds the data-remote: true attribute to any object you add it to, allowing Rails's UJS script to bind it to some ajax functionality...

Some definition from Rails:

Note the data-remote="true". Now, the form will be submitted by Ajax rather than by the browser's normal submit mechanism.

You probably don't want to just sit there with a filled out , though. You probably want to do something upon a successful submission. To do that, bind to the ajax:success event. On failure, use ajax:error. Check it out:

It's pretty simple really....

It basically assigns a Javascript .on bind to any elements on your page which have data-remote: true. This is what sends the Ajax request for you.


Hooks

The important thing to note is that this method creates several "hooks" which you can use with your other javascript:

This allows you to use the remote: true functionality, and customise what happens by using the hooks above...

#app/views/messages/new.html.erb
<%= form_for @messages, remote: true do |f| %>
   <%= f.submit %>
<% end %>

#app/assets/javascripts/application.js
$(document).on("ajax:success", "#messages", function(event, data, status, xhr) {
    alert("Form submitted, thank you!");
});

js

remote: true is a part of the Rails Ujs (unobtrusive Javascript) driver. It just adds the data-remote: true attribute to any object you add it to, allowing Rails's Ujs script to bind it to some ajax functionality...

钢轨的一些定义:

Note the data-remote="true". Now, the form will be submitted by Ajax rather than by the browser's normal submit mechanism.

You probably don't want to just sit there with a filled out , though. You probably want to do something upon a successful submission. To do that, bind to the ajax:success event. On failure, use ajax:error. Check it out:

真的很简单…

它基本上分配一个JavaScript。在您的网页上的任何元素绑定:真正的远程数据。这是送你的AJAX请求。


值得注意的是,此方法创建几个“钩子”,你可以用你的其他JavaScript:

这允许你使用远程:真正的功能,和自定义发生什么用钩子上面…

#app/views/messages/new.html.erb
<%= form_for @messages, remote: true do |f| %>
   <%= f.submit %>
<% end %>

#app/assets/javascripts/application.js
$(document).on("ajax:success", "#messages", function(event, data, status, xhr) {
    alert("Form submitted, thank you!");
});
answer2: 回答2:

remote: true generates data-remote="true" as html

It submits form by ajax rather than browser's normal submit mechanism

Reference

远程:真正产生数据远程=“true”为HTML

它通过Ajax提交表单而不是浏览器的正常提交机制

参考

answer3: 回答3:

:remote => true flag is used on the link or form tag in your view file for the element that you want to trigger the AJAX call, e.g.

<%= form_for @task, remote: true do |f| %>

with :remote => true, Rails will not automatically switch views, which allows the JQuery to be run instead.

The main purpose of using :remote => true with the form is, the form is submitted by Ajax rather than by the browser's normal submit mechanism.

I highly recommend you to read the Official Rails Documentation for Working with JavaScript in Rails. There you will find some examples of using :remote => true with forms, links etc which will help you understand the purpose of :remote=true in Rails :-)

:远程= & gt;真正的旗帜是用来链接或在你的视图文件的元素,你想触发Ajax调用的形式标记,如

<%= form_for @task, remote: true do |f| %>

:远程= & gt;真的,Rails不会自动切换视图,这使得jQuery运行相反。

使用目的:远程= & gt;真实的形式,形式是通过Ajax而不是浏览器的正常提交提交机制。

I highly recommend you to read the Official Rails Documentation for Working with JavaScript in Rails. There you will find some examples of using :remote => true with forms, links etc which will help you understand the purpose of :remote=true in Rails :-)

answer4: 回答4:

remote: true allows to submit a form or simply to generate a request without refreshing the page

You can use remote: true with links as well The best example of remote form submission is when you upvote/downvote an answer on stackoverflow

远程:真实允许提交表单或简单地生成请求而不刷新页面

You can use remote: true with links as well The best example of remote form submission is when you upvote/downvote an answer on stackoverflow

ruby-on-rails  ajax  ruby-on-rails-4