简体   繁体   中英

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.

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.

I googled some sample codes like this,

$("#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.

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?

My boss wants to make this html codes automatically using rails code, not making the tags manually what i've done.

I think rails form helper is only supporting for simple text fields or select box or input that kind of stuff. Do they support table also? to put tr and td tags?

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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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