Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8730597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:18+00:00 2026-06-13T09:03:18+00:00

I have a Rails 3.2.8 app in which I’m applying some AngularJS for calculations

  • 0

I have a Rails 3.2.8 app in which I’m applying some AngularJS for calculations dynamically within a form.

I have a basic test application which maps an investor to multiple houses and each house has a cost and a value in which I would like to total at the end.

Here is my form

<div ng-controller="InvestorCtrl">
  <%= form_for(@investor) do |f| %>
  <% if @investor.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@investor.errors.count, "error") %> prohibited this investor from being saved:</h2>

    <ul>
      <% @investor.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <% i = 0 %>
  <%= f.fields_for :houses do |builder| %>
  <%= builder.label :address %>
  <%= builder.text_field :address %>

  <%= builder.label :suburb %>
  <%= builder.text_field :suburb %>

  <%= builder.label :cost %>
  <%= builder.text_field :cost, "ng-model" => "timesheets[#{i}].cost", "ng-change" => "calc_cost()" %>

  <%= builder.label :value %>
  <%= builder.text_field :value, "ng-model" => "timesheets[#{i}].value" %>

  <% i = i + 1 %>

  <% end %>

  <div class="field">
    <%= f.label :total_cost %>
    <%= f.number_field :total_cost, "ng-model" => "total_cost" %>
  </div>
  <div class="field">
    <%= f.label :total_value %>
    <%= f.number_field :total_value, "ng-model" => "total_value" %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
  <% end %>
</div>

Here is the angularjs coffeescript I’m using

$ (event) ->
  app = angular.module "investor", []

  app.controller("InvestorCtrl", ["$scope", ($scope) ->
    $scope.timesheets = [
      { cost: 295000, value: 450000 },
      { cost: 600000, value: 620000  },
      { cost: 1000000, value: 900000 },    
    ]
    $scope.calc_cost = ->
      total = 0
      for ts in $scope.timesheets
        total = total + ts.cost
      $scope.total_cost = total

    $scope.calc_cost()
  ])

  angular.bootstrap document, ['investor']

When I load a new form I’m building three houses in the controller like so:

  def new
    @investor = Investor.new
    3.times { @investor.houses.build }

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @investor }
    end
  end

The total cost is calculated correctly when going to a new form, however when I change any of the houses ‘cost’ values the ‘total_cost’ field is set to blank.

Am I binding correctly?
Is there an easier way to bind nested forms using AngularJS with Rails templates?

At the moment I’m just trying to get a sum of houses ‘cost’ value into the ‘total_cost’ field using AngularJS.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:03:19+00:00Added an answer on June 13, 2026 at 9:03 am

    I managed to find my problem using the great debugging tool called ‘batarang’
    https://github.com/angular/angularjs-batarang

    I just had to parseInt the strings:

        total = parseInt(total) + parseInt(ts.cost)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got Sinatra/Rails app and an action which starts some long process. Ordinary
I have a rails app which is localized in multiple languages. Some time for
I have a rails app which is already running with some users, users that
I have a rails app in which I am trying to do some full
In my Rails app, I have a form which redirects through a foreign service,
I have a Ruby/Rails app, which is standard Rails CRUD, with some jQuery and
I have the following form in a rails app which is not working: <%
I have rails app which are working perfectly in the local computer. But when
So I have a Rails app (which in this case seems like it would
I have a tab navigation page in my rails app which is shared across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.