AngularJS 的新手。我很难让 RESTful 的东西在 angular 和 rails 之间工作。我设置了一个简单的休息服务并得到很好的帖子。绑定工作等。但是当我 $update back .. 发送到服务器的是整个 post 对象。我需要能够将其过滤到某些属性。此外,发布的内容并未包含在其中,params[:post]
这是典型的 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