2013-03-01 51 views
2

AngularJS的新功能。在角度和轨道之间运行RESTful的东西时遇到了一些麻烦。我设置了一个简单的休息服务,并获得罚款。绑定工作等等。但是,当我更新回来..发送到服务器的是整个post对象。我需要能够将其过滤到某些属性。此外,张贴的内容不包含在params[:post]这是典型的导轨方法。AngularJS和Rails休息设置

见下文:

# angular 
app.factory "Post", ["$resource", ($resource) -> 
    $resource "/posts_api/:id", {id: "@id"}, {update: {method: "PUT"}} 
] 

app.controller "PageEditCtrl", ($scope, Post) -> 
    $scope.post = Post.get(
    id: 32723 
    , -> 
    $scope.post.title = $scope.post.title + "!" 

    $scope.post.$update({id: $scope.post.id}) 
) 
...... 

# in rails Post.rb class 
attr_accessible :title, :body, :user_id 

# in rails posts_api_controller.rb 
class PostsApiController < ApplicationController 
    respond_to :json 
    before_filter :authenticate_user! 

    # **** HERE'S THE PROBLEM:::: 2 issues with updating 
    # 1) angular is passing *entire post object* with attributes that are *not in attr_accesible* 
    # 2) angular is not passing the post in the typical rails fashion params[:post] ..instead just as params 

    def update 
    respond_with current_user.posts.update(params[:id], params) # would like to have params[:post] instead here 
    end 
end 
+0

不知道这是否会帮助你,但有一个很好的投射角js轨道。 http://railscasts.com/episodes/405-angularjs虽然这一集是“专业”,所以要得到它,你必须注册(9美元月) – cgat 2013-03-02 02:56:58

回答

1

问题#2,这里就是我有我的AngularJS应用程序设置:

railsResources = 
    index: 
    method: "GET" 
    isArray: true 
    show: 
    method: "GET" 
    new: 
    params: 
     verb: "new" 
    method: "GET" 
    create: 
    method: "POST" 
    edit: 
    params: 
     verb: "edit" 
    method: "GET" 
    update: 
    method: "PUT" 
    destroy: 
    params: 
     command: "remove" 
    method: "DELETE" 

app.factory "Post", ["$resource", ($resource) -> 
    $resource "/posts_api/:id/:verb", {id: "@id"}, railsResources 
] 

这应该让你如你所期望传达给导轨。

我想我记得在Angular文档中看到你的例子,并认为它是一个人为的例子。 我认为你应该分开你的功能。 而且你不需要在ID传递,当你使用一个资源生成的对象:

$scope.post = Post.get(id: 32723) 
$scope.addBang = -> 
    $scope.post.title = $scope.post.title + "!" 
    $scope.post.$update() 

问题#1,想在Rails形式的条款。 如果你想避免通过受限制的参数,你只需将它们排除在表单之外。 然而,如果你想使用Angular内置的模型/资源接口,那么说起来容易做起来难。

所以我建议创建一个接受完整params [:post]哈希的Rails模型方法,并返回只有可访问方法的哈希。

通过这样做,您可以保持干爽,并避免在模型更改时调整JS。

编辑: 或者,更好的是,正如我刚刚学到的,使用Rails模型生成哈希,然后再将它传递给AngularJS。

像这样:

post = Post.find(123) 
post = { name: post.title, content: post.content } 
0

或者有一个已经实现了RESTful架构,并使用承诺的优秀rails-resource-angular gem。此外,发布请求将被正确包装,以便您可以使用params [:post]。

angular.module('book.services', ['rails']); 
angular.module('book.services').factory('Book', 
['railsResourceFactory', function (railsResourceFactory) { 
    return railsResourceFactory({ 
     url: '/books', 
     name: 'book' 
    }); 
}]); 

Book.query({ title: title }).then(function (results) { 
     $scope.books = results; 
     $scope.searching = false; 
    }, function (error) { 
     // do something about the error 
     $scope.searching = false; 
    }); 
Book.get(1234).then(function (book) { 

new Book({title: "Awesomeness", author: 123}).create() 
.then(function(book){ 
    $scope.book = book // Rails generated an ID for us, and everything else we put in our model 
}) 

关于attr_accessible的东西,你可能即使你不使用轨道4

想看看at this post因为,如果你想发送POST请求之前过滤,有些你”必须编写params.require()。的许可证()。或者,我可能不太了解这个问题......?