Rails app render external API json response in view after page load with Ajax -


I have a rail app that has many pages to pull information from the external API (Salesforce API). I have been successfully pulling data through the API, although I want to provide a better experience to my users and first want to load the scene, then call the API and load the AAPX response with Ajax. I have two applicable routes i first loads the scene and the second route requests the API

Get the routes.rb

  "Lmbit_on ', for:' lender_account #pending_loans get 'to,' pull_pending_loans': 'lender_account # pull_pending_loans'  

look like the same controller functions:

< P> lender_account_controller.rb

def pull_pending_loans @user = current_user if @ user.activated? Customer = Restforce.new @account = client.find ( 'Account', @ user.salesforce_id, 'Eid') pending_query = "Loan__r.Business_Name__r.Name, Loan__r.Id, Loan__r.Name, Loan__r.Loan_Risk__c, Interest_Rate__c, Amount_Lent__c selection render Status__c, Loan__r.Time_Left_on_Listing__c, Loan__r.Funded__c end where Account__c = '% s' and Status__c = 'oath' "% @ account.Id.to_s @pendingloanparts = client.query (pending_query) from Loan_Part__c: json = & gt ; @pendingloanparts def pending_loans @user = current_user end and the other end

In my view, I have the following: pending_loans.html.erb

 < Code> & lt; Script type = "text / javascript" & gt; $ .ajax ({url: "/ pull_pending_loans", cache: incorrect, breakthrough: function (html) {$ ("# pending logo"). Attachment (html);}}); & Lt; / Script & gt; & Lt; Div class = "profile container content" & gt; & Lt; Div class = "row" & gt; & Lt;% = Render 'Lenders / Sidebar'% & gt; & Lt ;! - Start the content - & gt; & Lt; Div class = "col-md-9" & gt; & Lt; Div class = "profile-body" & gt; & Lt ;! - Pending loan - & gt; & Lt; Div square = "panel panel-purple margin-down-40" id = "small" & gt; & Lt; Div class = "panel-title" & gt; & Lt; H3 class = "panel-title" & gt; & Lt; I class = "fa f-refresh" & gt; & Lt; / I & gt; Pending credit & lt; / H3 & gt; & Lt; / Div & gt; & Lt; Div class = "table-responsive" & gt; & Lt; Table class = "table-table-hover" id = "parallax list" & gt; & Lt; Thead & gt; & Lt; TR & gt; & Lt; Th & gt; Company & lt; / Th & gt; & Lt; Th & gt; Credit title & lt; / Th & gt; & Lt; Th & gt; Risk & lt; / Th & gt; & Lt; Th & gt; Rate & lt; / Th & gt; & Lt; Th & gt; Amount & lt; / Th & gt; & Lt; Th & gt;% funded & lt; / Th & gt; & Lt; Th & gt; Time left & lt; / Th & gt; & Lt; / TR & gt; & Lt; / Thead & gt; & Lt; Tbody & gt; & Lt; TD & gt; & Lt; Div id = "pending_loans" & gt; & Lt; P & gt; Loading .. & lt; / P & gt; & Lt; / Div & gt; & Lt; / TD & gt; & Lt; / Tbody & gt; & Lt; / Table & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt ;! - Pending loan - & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt ;! - Final content - & gt; & Lt; / Div & gt;  

From now on, when I load the page, the scene loads immediately, but I do not see Jason inside the pending debt plan. Besides, if I got the JSON with the list of records, how will I be repeated on them and fill the table properly?

You must use the method to execute AJAX calls when page loads.

Also console methods (WebKit and FF) Console.log () and console.debug (during script execution that is useful to view it) .

This way your AJAX calls Wrap:

  $ ( "/ pull_pending_loans", cache: false, success :; function () {console.log ( '! DOM ready ') // A preloader or something else $ .ajax ({url show function (html) {// evaluation of results, or JSON data console.log (' result found ', HTML); $ ("# pending_loans") Generate table with. Attachment (html); // Hide preloader}}}}  

Sending HTML from Ruby is not a good idea, you can return better Jasonone archive And to create a table for templates A. You can use a front-end framework.

Take a look at the

Comments

Popular posts from this blog

c# - NewtonSoft JArray - how to select multiple elements with LINQ -

c# - Process.Kill() returns access denied -

c# - Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments -