2

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
4

2 回答 2

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
]

这应该允许您按预期与 Rails 进行通信。

我想我记得在 Angular 文档中看到您的示例,并认为这是一个人为的示例。我认为你应该分开你的功能。当您使用资源生成的对象时,您不需要传入 id:

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

对于问题 #1,考虑 Rails 表单。如果您想避免传入受限参数,只需将它们排除在表单之外即可。但是,如果您想使用 Angular 的内置模型/资源接口,这说起来容易做起来难。

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

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

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

像这样:

post = Post.find(123)
post = { name: post.title, content: post.content }
于 2013-03-12T19:08:45.297 回答
0

或者有已经实现了 RESTful 架构并使用 Promise的优秀rails-resource-angular gem 。此外,post 请求将被正确包装,因此您可以使用 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 的东西,即使你没有使用 rails 4 ,你也可能想看看这篇文章。

因为,如果您想在发送 post 请求之前进行过滤,您将不得不编写一个 javascript 版本的 params.require().permit()。或者也许我没有很好地理解这个问题......?

于 2014-08-27T22:56:39.113 回答