简体   繁体   English

在Rails 4上处理AJAX的正确方法是什么?

[英]What is the right way to deal with AJAX on Rails 4?

On most tutorials out there, people tell you to create a js.erb template for every action you want to respond with javascript , which leads to having both an html.erb and a js.erb , for every action I want to work with AJAX, like this: 在大多数教程中,人们告诉您为要使用javascript响应的每个动作创建一个js.erb模板,这导致我想与AJAX一起使用的每个动作都同时具有html.erbjs.erb 。 , 像这样:

在此处输入图片说明

Is this right? 这是正确的吗? Am I doing something wrong? 难道我做错了什么? Because it looks awful to me, there will be at least 20 files in each view folder, by default. 因为对我来说看起来很糟糕,所以默认情况下每个视图文件夹中至少有20个文件。

I think you are doing right. 我认为您做对了。 You are using Rails' AJAX helper and it is a good practice. 您正在使用Rails的AJAX帮助程序,这是一个好习惯。 Some advantages of this comparing to the normal way of using AJAX: 与使用AJAX的常规方法相比,此方法的一些优点:

  • JS code is shorter and cleaner, we do not need to write some repeated boring code such as $("form#id").on("submit", function(){}). JS代码更短,更干净,我们不需要编写一些重复的无聊代码,例如$(“ form#id”)。on(“ submit”,function(){})。 We just need to write the main code to handle the response data. 我们只需要编写主要代码即可处理响应数据。
  • Unobtrusive JavaScript: JS code is rendered from server side. 不引人注目的JavaScript:JS代码是从服务器端呈现的。 It is not shown along with the html. 不会与html一起显示。
  • I think splitting to js.erb files actually makes the code more manageable. 我认为拆分为js.erb文件实际上使代码更易于管理。 It is personal thought though. 这是个人的想法。

I don't know how complex your project is so I am not sure but maybe you can refine to have less partial files. 我不知道您的项目有多复杂,所以不确定,但是也许您可以改进以减少部分文件。 For example, I noticed that you have both delete and destroy actions. 例如,我注意到您同时具有删除销毁操作。 Index , new and edit views may not need the partial files. Indexnewedit视图可能不需要部分文件。 It seems that you also handle Json requests. 看来您也可以处理Json请求。 It also makes the view folder bigger. 这也使视图文件夹更大。

Yeah, it looks really awful to me too. 是的,对我来说真的也很糟糕。 But if you're responding to every method with javascript you'll have to create js.erb templates for each them. 但是,如果要使用javascript响应每种方法,则必须为每个方法创建js.erb模板。

Another approach would be, you'd want to respond with json instead of script. 另一种方法是,您希望使用json而不是脚本进行响应。 Where all your ajax code will remain in the client side javascript, and you'll be responded back with json data. 您所有的ajax代码都将保留在客户端javascript中,并且将以json数据作为响应。

For eg. 例如。 lets get data for an particular area 让我们获取特定区域的数据

$.ajax({
  url: "/areas/23",
  dataType: 'json',
  method: 'get',
  success: function(response){
    //OPTION 1
    //response will have all the data
    //handle the value from the response object in displaying


    //OPTION 2
    //If you set dataType: 'html' you can receive html partial from server and make use of it directly
    $("#show-area").html(response); //response will have content of _show.html.erb
  },
  error: function(error){
    console.log(error); //Print errors if it breaks
  }
});


#Controller
def show
  respond_to do |format|
    #OPTIONS 1
    format.json { render json: @area.as_json } 
    #Or have a json.jbuilder partial if you want to send data selectively, ;) There is no escape from partials/templates

    #OPTION 2
    format.html { render partial: "areas/show" } # will render _show.html.erb

  end
end

That being said, I think it finally comes to personal preference. 话虽如此,我认为这最终取决于个人喜好。 And your preferences will vary upon different scenarios. 您的偏好会因不同的情况而异。 You can pick any one of those based on the case. 您可以根据情况选择任何一种。 Let me know if it helped. 让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM