简体   繁体   English

jQuery的Rails问题中的AJAX

[英]AJAX in Rails Issue with jQuery

Right now, i'm requesting a user list from the controller side, and make the html code and append it. 现在,我正在从控制器端请求用户列表,并制作html代码并将其附加。

i made the code like this, 我做了这样的代码,

$.ajax({

        type : "get",
        contentType : "application/json; charset=utf-8",
        url : "/users/search_users",
        data : {
            name : name
        },
        dataType : "text",
        success : function(result) {
            if (result == "User not found") {
                alert("User not found");
            } else {
                //console.log(result);
                var peopleData = jQuery.parseJSON(result);
                var resultHTML = "<tr>";
                resultHTML += "<th></th><th style='display:none'>User ID</th>" + "<th>First Name</th><th>Last Name</th><th>Email Address</th>" + "<th style='display:none'>Time Zone</th>";
                resultHTML += "</tr>";
                $.each(peopleData, function(index, obj) {

                    resultHTML += "<tr>";
                    resultHTML += "<td><input type='checkbox'></td>" + "<td style='display:none;'>" + obj.id + "</td>" + "<td>" + obj.firstname + "</td>" + "<td>" + obj.lastname + "</td>" + "<td>" + obj.email + "</td>" + "<td style='display:none;'>" + "Etc/GMT+9 (Japan)" + "</td>";
                    //consider now
                    resultHTML += "</tr>";

                });

                $("#internal_table").html(resultHTML);
            }
        },
        error : function() {
            window.alert("something wrong!");
        }
    });

and it works fine, but my boss wants to change this code, using $.load and rails code. 并且工作正常,但我的老板想使用$ .load和rails代码更改此代码。

I googled some sample codes like this, 我在Google上搜索了一些示例代码,

$("#ad_ad_ad_ad_type_id").load('/ad_ad_types/ajax_types?adtype='+$("#ad_ad_adtype").val());

form_rows << [f.label(:screen_type, t('ad.ad_type')), f.select(:ad_ad_type_id, @ad_ad_types.map{|st|[st.description, st.id]}, {:selected => @ad_ad.ad_ad_type_id})]

this sample code is, getting the drop down box data from the controller side, 该示例代码是从控制器端获取下拉框数据,

and using form.select it is making the <option>data</option> automatically in the view side. 然后使用form.select在视图侧自动生成<option>data</option>

In my case, is it possible to put tr and td tags with specific attrubites using rails code , like i've done at the first code? 就我而言,是否可以像我在第一个代码中所做的那样,使用rails代码将tr和td标签放在特定的属性上?

My boss wants to make this html codes automatically using rails code, not making the tags manually what i've done. 我的老板想使用Rails代码自动创建此html代码,而不是手动完成标签。

I think rails form helper is only supporting for simple text fields or select box or input that kind of stuff. 我认为Rails表单帮助程序仅支持简单的文本字段或选择框或输入此类内容。 Do they support table also? 他们也支持餐桌吗? to put tr and td tags? 把tr和td标签?

Any good solution? 有什么好的解决办法吗?

I am not a big fan of form helper but here is what i would do, 我不是表单助手的忠实粉丝,但是我会做的是,

<%= form_for(< your config here >) do |f|%>
    <tr>
        <td>
            <%= f.label(:screen_type, t('ad.ad_type')) %>
        </td>
        <td>
            <%= f.select(:ad_ad_type_id, @ad_ad_types.map{|st|[st.description, st.id]}, {:selected => @ad_ad.ad_ad_type_id}) %>
       </td>
    </tr>
<% end %>

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

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