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 7945537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:53:43+00:00 2026-06-04T00:53:43+00:00

I’m building a simple Contact Management app with Crud using AngularJS 1.0.0rc8. Getting a

  • 0

I’m building a simple Contact Management app with Crud using AngularJS 1.0.0rc8.

Getting a list of currently existing contacts is no problem, but while attempting to save a new Contact to the server, a new row is created – complete with the correct id, created_at, and updated_at values – but the rest of the models data is ignored.

Here is a screenshot to show what I mean:

enter image description here

As you can see, numbers 4 and 5 were given the Id’s but first_name, last_name, and phone_num were not saved to the database.

I am using a $scope.addContact function within the Controller that deals with the object.

Here is the entire code for the Contact List Controller:

'use strict';

function ContactListCtrl($scope, $http) {
  $http.get('/contacts').success(function(data) {
    $scope.contacts = data;
  });    

  $scope.addContact = function(data) {      

    $http.post('/contacts/', data).success(function(data) {
        console.log(data);
        data.first_name = $("#new_contact_first_name").val();
        data.last_name = $("#new_contact_last_name").val();
    });

    this.newFirstName = '';
    this.newLastName = '';
  };

};

After clicking ‘Save’ on the new-contact.html partial, the Object is logged to the Console, if I inspect its contents, than sure enough the values are collected – notice Jimi Hendrix is there:

enter image description here

Here is the form as it appears in the new-contact.html template:

<form id="contact_form" ng-submit="addContact()">
            <input type="text" id="new_contact_first_name" name="newContactFirstName" ng-model="newFirstName" placeholder="First Name"></br>
            <input type="text" id="new_contact_last_name" name="newContactLastName" ng-model="newLastName" placeholder="Last Name"></br>
            <input type="button" id="contact_submit_btn" value="Add Contact" class="btn btn-primary">               
        </form>

The addContact() function is fired after the form is submitted with JQuery:

$(document).ready(function(){       

        $("#contact_submit_btn").click(function(){          
            $("#contact_form").submit();
        });
    });

(Something tells me that I may not be using the ng-model attributes correctly.)

Any ideas on where I am going wrong with this? Or ideas on how I can better go about implementing this design?

Thanks.


UPDATE BELOW:

Here is my entire updated controller code – with help from Sathish:

 // contacts controllers

'use strict';

function ContactListCtrl($scope, $http, Contacts) {   

  $scope.contacts = Contacts.index();  

  $scope.addContact = function() {
        var newContact = {
            first_name: $scope.newContactFirstName,
            last_name: $scope.newContactLastName
        };

        var nc = new Contacts({ contact: newContact });

        nc.$create(function() {
            $scope.contacts.push(nc);
            // now that the contact is saved, clear the form data
            $scope.newContactFirstName = "";
            $scope.newContactLastName = "";
            })
        }

};

ContactListCtrl.$inject = ['$scope', '$http', 'Contacts'];


function ContactDetailCtrl($scope, $routeParams, Contacts) {  
    $scope.contact = Contacts.get( {contact_id: $routeParams.contact_id} ); 
}

ContactDetailCtrl.$inject = ['$scope', '$routeParams', 'Contacts'];

I am now receiving the error: Unknown Provider for Contacts. Here is a screenshot of the error

Ok, I managed to fix that error by providing a ngResource to the main App file. Here’s what it looks like:

// main app javascript file

'use strict';

angular.module('contactapp', ['ngResource']).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/contacts', {template: 'assets/app/partials/contact-list.html',   controller: ContactListCtrl}).
      when('/contacts/new', {template: 'assets/app/partials/new-contact.html',   controller: ContactListCtrl}).
      when('/contacts/:contact_id', {template: 'assets/app/partials/contact-detail.html', controller: ContactDetailCtrl}).
      otherwise({redirectTo: '/contacts'});
}]);

I am receiving a new error: WARNING: Can’t verify CSRF token authenticity

Alright, managed to fix that problem too by adding a callback to the API controller:
Rails shows "WARNING: Can't verify CSRF token authenticity" from a RestKit POST

Now I am back to the original problem. When the create method is called, a new row is saved to the database, but the models data is not.

Awesome… finally got this thing working.

The problem was the $scope.addContact function. It was using the ‘name’ of the input instead of the ng-model binding called ‘newFirstName’ and ‘newLastName’ that resides in the template.

Here’s what the updated function looks like:

$scope.addContact = function() {
        var newContact = {
            first_name: $scope.newFirstName,
            last_name: $scope.newLastName
        };

        var nc = new Contacts({ contact: newContact });

        nc.$create(function() {
            $scope.contacts.push(nc);
            // now that the contact is saved, clear the form data
            $scope.newFirstName = "";
            $scope.newLastName = "";
            })
        }
  • 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-04T00:53:45+00:00Added an answer on June 4, 2026 at 12:53 am

    This can be better implemented using a Contacts service. Please define a Contacts service in app/assets/javascripts/services.js.erb as shown below:

    var servicesModule = angular.module('<your app name>',
      [<list of modules needed by this app>]);
    
    servicesModule.factory('Contacts', function($resource) {
      var ContactsService = $resource('/contacts/:contact_id', {}, {
        'create': { method: 'POST' },
        'index': { method: 'GET', isArray: true },
        'update': { method: 'PUT' },
        'destroy': { method: 'DELETE' }
      });
      return ContactsService;
    });
    

    Change the addContact method in the controller as shown below:

      function ContactListCtrl($scope, $http, Contacts) {
        ...
        ...
        ...
        $scope.addContact = function () {
          var newContact = {
            first_name: $scope.newContactFirstName,
            last_name: $scope.newContactLastName
          };
    
          var nc = new Contacts({ contact: newContact });
          nc.$create(function() {
            $scope.contacts.push(nc);
            // now that the contact is saved, clear the form data.
            $scope.newContactFirstName = "";
            $scope.newContactLastName = "";
          });
        };
        ...
        ...
        ...
      }
      ContactListCtrl.$inject = ['$scope', '$http', 'Contacts'];
    

    In addition to this, you can simplify the $http.get(...) part also. You can use Contacts.index();

    Note:

    • If you gave ng-app="MyAppName" then please replace <your app name> with MyAppName.
    • <list of modules needed by this app> needs to the replaced by a comma-separated list of strings representing any modules needed by your application.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.