简体   繁体   English

在.js.erb文件的Ajax调用中创建RoR链接

[英]Creating RoR Links within Ajax call in .js.erb file

I have the following ajax call that activates on button click: 我有以下ajax调用,可在单击按钮时激活:

    $.ajax({

            url: button_action,

            data: {},

            type: "POST",

            dataType: "json",

            success: function( data ) {

                var att = data.attendees

                var attendee_list = "";

                for (i = 0; i < data.attendees.length; i++) {
                    if ( i != att.length-1) {
                        attendee_list += att[i].first_name + " " + att[i].last_name + ", ";
                    }       else {
                        attendee_list += att[i].first_name + " " + att[i].last_name 
                    }
                }

                att_array.innerHTML = attendee_list;
            }, . . . 

        });

Now, we see that I'm getting a list of event attendees back from the server in data.attendees . 现在,我们看到我从data.attendees的服务器中获取了事件参与者的列表。 I'm able to get the first and last name of each attendees, and I can just as easily get the id of the attendees with id = att[i].id . 我可以获取每个与会者的名字和姓氏,并且可以轻松获得id = att[i].id的与会者id = att[i].id Instead of just displaying the name of each attendee, I would like to create a link to that attendees show page using their id . 我不仅要显示每个与会者的姓名,还想使用其id 创建指向该与会者show页面的链接 Ideally, I would create a variable full_name = att[i].first_name + " " att[i].last_name and then create a link like 理想情况下,我将创建一个变量full_name = att[i].first_name + " " att[i].last_name ,然后创建一个类似

<%#= link_to full_name, user_path(user.id) %>

I am aware that this involves taking json or javascript variables and using them in Ruby code, and then outputting Ruby code. 我知道这涉及获取json或javascript变量并在Ruby代码中使用它们,然后输出Ruby代码。 Is this possible, and if so, how can I do it? 这可能吗?如果可以,我该怎么办?

The way your are doing it, with a simple AJAX request, is impossible (by design). 通过简单的AJAX请求,您的操作方式是不可能的(通过设计)。 The problem is that the Ruby code renders first (since it is server-side) and the JavaScript renders after it. 问题在于,Ruby代码首先呈现(因为它是服务器端的),而JavaScript呈现之后的。 So you can't really 'inject' json in a Ruby method because the helper link_to will execute before the JavaScript is even requested from the server. 因此,您实际上无法在Ruby方法中“注入” json,因为帮助程序link_to将在服务器甚至请求JavaScript之前执行。

What you would like to have it is using unobtrusive JavaScript (UJs). 您想要使用的是非侵入式JavaScript(UJ)。 It is done by putting :remote => true on your link. 通过在链接上放入:remote => true可以完成此操作。 After doing so, the request will go to the server-side method put in your link ( attendees ). 这样做之后,请求将转到链接( attendees )中放置的服务器端方法。 Server side, you need to have a respond_to block: 服务器端,您需要有一个respond_to块:

def attendees
  @attendees = Attendee.all #I'm making this up for the example
  respond_to do |format|               
    format.js
  end        
end 

Then, you need a .js.erb file named after the action, ie attendees.js.erb . 然后,你需要一个.js.erb动作后,即命名的文件attendees.js.erb It will be called when the server-side gets executed. 在服务器端执行时将调用它。 You will receive an array of items. 您将收到一系列物品。 You need a partial that is going to iterate through them. 您需要局部迭代它们。 Here is how you are going to do it. 这是您要执行的操作。 In attendees.js.erb attendees.js.erb

('.attendee-list-container').html("<%= j render(:partial => 'attendees_list', :locals => { :attendees => @attendees }) %>");

And finally, in your _attendees_list.html.erb , you can iterate and create links. 最后,您可以在_attendees_list.html.erb中进行迭代并创建链接。

#in HAML:
-attendees.each do |attendee|
  =link_to "#{attendee.first_name + ' ' + attendee_last_name}" , attendee

That's it. 而已。

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

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