В моем приложении Ruby on Rails я пытаюсь добавить новую запись через AngularJS. Я использую этот метод:Ruby on Rails - AngularJS добавить новую запись
я создать ресурс под названием уведомление:
rails g resource notifications title:string body:text
и я заполнить ее:
Notification.create!(title: "Test1", body:" Some Text here")
Notification.create!(title: "Test2", body:" Some Text here")
Notification.create!(title: "Test3", body:" Some Text here")
Notification.create!(title: "Test4", body:" Some Text here")
я передать все данные по Json (находится в http://localhost:3000/application/notifications)
я установить теперь некоторые угловые ЯШ:
app.controller('notificationCtrl', ['$scope', '$http', function ($scope, $http) {
// array for the notifications
$scope.notifications = [];
// method for get the notification
var retriveNotifications = function(){
// Console Log
console.log(' Sto preparando le notifiche ');
// trying to get to the notification page
$http.get('/application/notifications').success(
// getting the notifications
function(items){
$scope.notifications = items;
});
};
//add a notification
$scope.updateNotifications = function(title, body){
var item = {title: title , body: body}
$http.post('/application/notifications', item);
}
retriveNotifications();
}])
это мой HTML-файл:
<form ng-submit="updateNotification()">
<input ng-model="notificationName" placeholder="Title of the notification">
<input ng-model="notificationText" placeholder="Body of notification">
<input type="submit" value="Send">
</form>
<ul ng-repeat="notification in notifications">
<li>
<h4>{{notification.title}}</h4>
<p>{{notification.body}}</p>
</li>
</ul>
Я новичок угловыми JS и я знаю, что функция updateNotifications неправильно. Как я могу вставить (в базе данных Notification) новую запись с Angular JS?
Спасибо за помощь
в форме, вам вызовите 'updateNotification()', но в контроллере метод называется 'updateNotifications'. Не уверен, что это единственная проблема, но первое, что вышло мне. –