2013-03-01 49 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()。的許可證()。或者,我可能不太瞭解這個問題......?