0

My rails app is making external queries to Salesforce through their API to pull some records. When I receive those records in the controller, I would like to use the number_to_currency and link_to helper on some of the fields before sending the JSON to my view. The records are then rendered in a table to be displayed. I understand these modifications will have to take place in the controller but I'm not sure how to do them before the render json: @pendingloanparts runs.

Previously the table looked like:

<td><%= loanpart.Loan__r.Business_Name__r.Name %></td>
<td><%= link_to loanpart.Loan__r.Name, loan_path(loanpart.Loan__r.Id) %></td>
<td><%= loanpart.Loan__r.Loan_Risk__c %></td>
<td><%= loanpart.Interest_Rate__c %>%</td>
<td><%= number_to_currency(loanpart.Amount_Lent__c) %></td>
<td><%= loanpart.Loan__r.Funded__c %>%</td>
<td><%= loanpart.Loan__r.Time_Left_on_Listing__c %></td>

I am now using AJAX to return the data from the salesforce API when the response is received.

the view:

<script type="text/javascript">
  $.ajax({
    url: "/pull_pending_loans",
    dataType: "json",
    cache: false,
    success: function(data){
      $('#ajax-loader').hide()
      $.each(data, function(index, element) {
        console.log(element)
        $('#pendingloanlist tbody').append("<tr><td>" + element.Loan__r.Business_Name__r.Name + "</td><td>" 
                                                      + element.Loan__r.Name + "</td><td>"
                                                      + element.Loan__r.Loan_Risk__c + "</td><td>"
                                                      + element.Interest_Rate__c + "%" + "</td><td>"
                                                      + element.Amount_Lent__c + "</td><td>"
                                                      + element.Loan__r.Funded__c + "%" + "</td><td>"
                                                      + element.Loan__r.Time_Left_on_Listing__c + "</td></tr>");
      });
    }
  });
</script>

<div class="profile container content">
  <div class="row">
    <%= render 'lenders/sidebar' %> 
      <!-- Begin Content -->
        <div class="col-md-9">
          <div class="profile-body">
            <!-- Pending Loans -->
              <div class="panel panel-purple margin-bottom-40" id="small">
                <div class="panel-heading">
                  <h3 class="panel-title"><i class="fa fa-refresh"></i> Pending Loans</h3>
                </div>
                <div class="table-responsive">
                  <table class="table table-hover" id="pendingloanlist">
                    <thead>
                      <tr>
                        <th>Company</th>
                        <th>Loan Title</th>
                        <th>Risk</th>
                        <th>Rate</th>
                        <th>Amount</th>
                        <th>% Funded</th>
                        <th>Time Left</th>
                      </tr>
                    </thead>
                    <tbody>
                      <td colspan="100" id="ajax-loader">
                        <center><%= image_tag("loading.gif") %></center>
                      </td>
                    </tbody>
                  </table>
                </div>
              </div>                  
            <!-- Pending Loans -->
          </div>
        </div>
      <!-- End Content -->
    </div>
</div>

The controller actions:

def pull_pending_loans
  @user = current_user
  if @user.activated?
    client = Restforce.new
    @account = client.find('Account', @user.salesforce_id, 'Id')
    pending_query = "select Loan__r.Business_Name__r.Name, Loan__r.Id, Loan__r.Name, Loan__r.Loan_Risk__c, Interest_Rate__c, Amount_Lent__c, Status__c, Loan__r.Time_Left_on_Listing__c, Loan__r.Funded__c from Loan_Part__c where Account__c ='%s' AND Status__c = 'Pledge'" % @account.Id.to_s
    @pendingloanparts = client.query(pending_query)
    render json: @pendingloanparts
  end
end 

def pending_loans
  @user = current_user
end
Questifer
  • 1,113
  • 3
  • 18
  • 48

1 Answers1

1

First: according to the MVC paradigm, formating should be done in the view.
To achive this, Rails use builders to render almost any kind of output format.

You can use jbuilder to use jbuilder views to render JSON and in this views of type ...json.jbuilder you can use view helpers as in any other view.

Said this, you can also break the MVC rules and include the helpers in the controller. See this answer.

Community
  • 1
  • 1
Martin M
  • 8,430
  • 2
  • 35
  • 53
  • Jbuilder looks interesting. I'm not sure how I would include the json.jbuilder view inside my normal view.html.erb file and have it formatted in a table properly? – Questifer Oct 17 '14 at 22:50
  • Jbuilder has solved my problem perfectly. Thanks for the recommendation and explanation. – Questifer Oct 18 '14 at 13:09