导轨:如何更新的has_many:经由jQuery的关系? [英] rails: how to update a has_many :through relation via jQuery?

查看:151
本文介绍了导轨:如何更新的has_many:经由jQuery的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们! 很抱歉,如果这是一个noobish的问题,但我刚开始使用Rails和jQuery的。我有以下情形:

Hey guys! Sorry, if this is a noobish question, but I'm just getting started with Rails and jQuery. I have the following scenario:

我有3类:联系人,公司和contact_company_joins(CCJ)。 对于所有三个类别,我创建的模型,控制器和视图。显然,联系人和 公司(使用的has_many:通过)通过连接表CCJ连接。

I have 3 classes: contacts, companies and contact_company_joins (ccj). For all three classes I created models, controller and views. Obviously, contacts and companies are connected via the join-table ccj (using has_many :through).

联系和LT; - > contact_company_joins< - >公司

contacts <-> contact_company_joins <-> companies

在我接触的秀的观点我想显示一个表,其中列出了触点连接到所有公司(以及公司本身,例如电话号码一些额外的信息)。在我公司的观点我想这样做,但这次与员工(接触)的详细信息。 该表的每一行具有典型的删除末的链接,其功能现在我已经迷上了我的jQuery的功能之一:

In my contact's "show" view I want to display a table that lists all companies that the contact is connected to (and some additional info on the company itself e.g. phone-number). In my company's view I want to do the same, but this time with the employees' (contacts') details. Each row of the table has the typical "delete" link at the end, whose function I have now hooked up to one of my jQuery functions:

$('.edit_contact_join_delete').livequery('click', function() {
    var $deleteButton = $(this);
    var answer = confirm("Sure?");
    var dataloc = "&_method=delete";
    if (answer) {
     $.ajax({
          type : "POST",
          url  : this.href,
          data : dataloc,
          success: function(result) {
          }
        });
    }
    return false;
  });

在这两种观点(联系人和公司),删除操作带我到CCJ控制器的破坏作用。 我想现在要做的是更新或重新呈现个别网站(个人或公司)正在调用删除功能的表,但表中只有一部分。我想我必须在成功的功能上面实现它?但话又说回来,即使我返回的东西从CCJ的控制器(所呈现的部分?)......这将是相同的两个联系人和公司认为,对不对? 我如何才能确保后,我的Ajax调用破坏CCJ对象,我得到了我的联系的观点,一个新的DOM对象(这将是一个新的公司表),而且还可以得到一个新的联系人表时,我做的Ajax调用我公司有何看法?

In both views (contact and company), the delete action takes me to the destroy function of the ccj controller. What I want to do now is to update or rerender the table of the individual site (contact or company) that is calling the delete function, but ONLY the part of the table. I guess I must implement it in the "success" function above? But then again, even if I return something from the ccj's controller (a rendered partial?)...that would be the same for both contact and company view, right? How can I make sure that after my Ajax call to destroy the ccj object, I get a fresh DOM object for my contact's view (which would be a new company table), but also get a fresh contacts table when I do the Ajax call from my company view?

希望这一切是有道理的: - )

Hope this all makes sense :-)

问候,

塞巴斯蒂安

推荐答案

所有你需要的是删除操作返回成功或失败(甚至只是真或假的都行),那么你的JavaScript可与响应处理。

All you need is for the delete action to return a success or failure (even just true or false will do), then your javascript can deal with either response.

如果失败则返回一个消息,说是这样,如果取得成功,关系删除,你会发现在&lt; TR&GT;父母,你点击,淡出排出来的删除链接,最终从页面删除。例如(假设如果删除成功,否则为false你的控制器返回true):

If a failure then return a message saying so, if a success and the relationship deleted, you could find the <tr> parent of the delete link that you clicked and fade the row out, eventually deleting it from the page. For example (given that your controller returns true if the delete is successful and false otherwise):

$('.edit_contact_join_delete').livequery('click', function() {
    var $deleteButton = $(this);
    var answer = confirm("Sure?");
    var dataloc = "&_method=delete";
    if (answer) {
     $.ajax({
          type : "POST",
          url  : this.href,
          data : dataloc,
          success: function(result) {
            if (result) {
              // handle successful delete
              $deleteButton.parent('tr').fadeOut().remove()
            } else {
              // handle failed delete
              alert('Delete failed');
            }
          } 
        });
    }
    return false;   
  });

这篇关于导轨:如何更新的has_many:经由jQuery的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆