TypeError:$ http.movi​​e不是函数

时间:2015-08-23 11:28:46

标签: ruby-on-rails angularjs

我正在尝试使用Angular和Rails将电影标题保存到我的数据库中,但是我遇到了一些错误,我想我知道问题是什么,但我不知道如何修复它。 / p>

设置,

这是我的搜索模板。当用户点击addMovie div时,addMovieCtrl会触发并尝试保存输入。

%div{"ng-controller" => "searchCtrl", :id => "container_search"}
  #addMovie{"ng-controller" => "addMovieCtrl"}
    %div{"ng-click" => "addMovie()"}
      %input{:type => "text", "ng-model" => "title"}
      addMovie action.

这是movieController.js,

angular.module('addMovieseat', ['ui.router', 'templates'])

  .controller('addMovieCtrl', ['$scope', 'movies',
    function($scope, movies){
      $scope.movies = movies.movies;

      $scope.addMovie = function(){

        console.log ("Hello")

        if(!$scope.title || $scope.title === '') { return; }
        movies.create({
          titles: $scope.title,
        });
        $scope.title = 'Star wars';
      };
    }
  ])

这是movieService.js

angular.module('addMovieseat')

  .factory('movies', ['$http', function($http){
    var o = {
      movies: []
    };

    o.create = function(movie){
      return $http.movie('/movies.json', movie).success(function(data){
        o.movies.push(data);
      });
    };

    return o;

  }])

Rails中的movie.rb模型

class Movie < ActiveRecord::Base

  def as_json(options = {})
  end

end

我的app.js.coffee,

@myApp        = angular.module('myApp',[]);
@flapperNews  = angular.module('flapperNews',[]);
@app          = angular.module('movieseat', ['addMovieseat','myApp', 'flapperNews', 'ui.router', 'Devise']);

# for compatibility with Rails CSRF protection

@app.config([
  '$httpProvider', ($httpProvider)->
    $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])

@app.run(->
  console.log 'angular app running'
)

当我点击addMovie div时,我收到此错误,

TypeError: $http.movie is not a function at Object.o.create

我已经完成了关于thinkster.io的Angular / Rails教程,我注意到的是当我去localhost:3000/posts.json时,我得到一个包含我创建的所有帖子的页面(在教程中) 。但当我去localhost:3000/movies.json时,我得到No route matches [GET] "/movies.json"

1 个答案:

答案 0 :(得分:2)

当然不是。

替换

$http.movie('/movies.json', movie)

使用:

$http.post('/movies.json', movie)

Sidenote,haml允许你编写好的html道具,如:

%div( ng-click="addMovie()" )