javascript js.erb 是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4606855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:16:57  来源:igfitidea点击:

How does js.erb work

javascriptruby-on-railsrubyasp.net-ajaxerb

提问by Matt Elhotiby

Lately i have run into a few applications that are using js.erband i am not really sure how to use it ...here is the code below. Can someone help me understand how this works?

最近我遇到了一些正在使用的应用程序,但js.erb我不确定如何使用它……这是下面的代码。有人可以帮助我了解这是如何工作的吗?

in the routes.rbfile

routes.rb文件中

map.resources :player_emails

my controller player_emails_controller.rbin the create action

player_emails_controller.rb在创建操作中的控制器

def create
 @player_email = PlayerEmail.create(params[:player_email])
 if @player_email.save
  @response_txt = "The player has been emailed."
  PlayerEmailsMailer.deliver_pattern_email(@something, @player_email, request.host_with_port)
  @error = false
 else
  @error = true
  @response_txt = "Please make sure you entered your name and a valid email address."
 end
end

then i have the file player_emails/create.js.erb

然后我有文件 player_emails/create.js.erb

$('#player_email_ind').hide();
$('#player_email_submit').show();
$('#player_response_msg').html("<%= escape_javascript @response_txt %>").fadeIn();
<% unless @error %>
$('#player_email_form')[0].reset();
<% end %>

i know what the jquery is going but i dont know how this is doing the ajaxcall. Does it just automatically do an ajaxcall when there is a js.erb...can someone explain the way this works and why i dont need a respond_to in the controller action telling it this is format.js

我知道 jquery 是怎么回事,但我不知道这是如何进行ajax调用的。它是否只是ajax在有js.erb...时自动拨打电话......有人可以解释它的工作方式以及为什么我不需要在控制器动作中使用respond_to来告诉它这是format.js

回答by mark

If a js (ajax) request is made it will respond by rendering the js.erb file and viceversa.

如果发出 js (ajax) 请求,它将通过呈现 js.erb 文件来响应,反之亦然。

This is the default behaviour that is being performed:

这是正在执行的默认行为:

  respond_to do |format|
    format.js{
      render :template => 'create.js.erb'
    }
    format.html{
      render :template => 'create.html.erb'
    }
  end

回答by zetetic

When the form is submitted, it does a POST to /player_emails. The resource declaration in routes.rbensures the request is handled by PlayerEmailsController#create.

提交表单后,它会向/player_emails. 中的资源声明routes.rb确保请求由 处理PlayerEmailsController#create

The controller is responsible for handling each format it receives. In the case of an AJAX call, the format is 'js', and is set by explicitly adding the format string to the end of the URL (/player_emails.js) or (more likely) by deducing the format from the request header.

控制器负责处理它接收到的每种格式。在 AJAX 调用的情况下,格式为“js”,并通过将格式字符串显式添加到 URL ( /player_emails.js) 或(更有可能)通过从请求标头中推断格式来设置。

In your case, the createaction does not expect anything other than AJAX, so it takes a shortcut and omits the respond_toand formatblocks. The controller has already figured out that the format is 'js', so when createis complete it takes the default action of rendering the appropriate template for the format (create.js.erb).

在您的情况下,该create操作不需要 AJAX 以外的任何内容,因此它采用了快捷方式并省略了respond_toformat块。控制器已经确定格式是 'js',所以当create完成时,它采取默认操作为格式 ( create.js.erb)呈现适当的模板。

回答by Samo

Does your form submit button have a :remote => trueon it? If so, there might be some JavaScript in rails.jsor application.jsthat automatically submits via AJAX. Bottom line is, there has to be some JavaScript somewhere that is making an AJAX call and asking for a js or JSON response, otherwise it would be an html request.

你的表单提交按钮:remote => true上有吗?如果是这样,则可能有一些 JavaScriptrails.jsapplication.js通过 AJAX 自动提交。底线是,某处必须有一些 JavaScript 进行 AJAX 调用并请求 js 或 JSON 响应,否则它将是一个 html 请求。

As for why you don't need a respond_toblock, I'm not entirely sure. Maybe since the call is always being made by AJAX and there is a js.erb template available, it just does its thing without complaining. Is there an html.erb template at all? If not, try doing a regular form submit and see if it complains.

至于为什么不需要respond_to块,我不完全确定。也许因为调用总是由 AJAX 进行并且有一个 js.erb 模板可用,它只是做它的事情而不会抱怨。是否有 html.erb 模板?如果没有,请尝试进行常规表单提交,看看它是否会抱怨。