Ruby on rails AngularJS和Rails rest设置

Ruby on rails AngularJS和Rails rest设置,ruby-on-rails,rest,angularjs,Ruby On Rails,Rest,Angularjs,AngularJS的新成员。我有一些麻烦,让休息的东西之间的角度和轨道工作。我设置了一个简单的休息服务,并得到了罚款后。绑定工作等,但当我更新回来。。发送到服务器的是整个post对象。我需要能够过滤下来,只是某些属性。此外,发布的内容没有包装在params[:post]中,这是典型的rails方法 见下文: # angular app.factory "Post", ["$resource", ($resource) -> $resource "/posts_api/:id", {id

AngularJS的新成员。我有一些麻烦,让休息的东西之间的角度和轨道工作。我设置了一个简单的休息服务,并得到了罚款后。绑定工作等,但当我更新回来。。发送到服务器的是整个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
#角度
app.factory“Post”、[“$resource”、($resource)->
$resource“/posts_api/:id”,{id:@id},{update:{method:“PUT”}
]
app.controller“PageEditCtrl”,($scope,Post)->
$scope.post=post.get(
身份证号码:32723
, ->
$scope.post.title=$scope.post.title+“!”
$scope.post.$update({id:$scope.post.id})
)
......
#在rails Post.rb类中
属性可访问:标题、正文、用户id
#在rails posts\u api\u controller.rb中
类PostsApiController
关于第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通信

我记得我在《角度文档》中看到你的例子,我认为这是一个人为的例子。 我认为你应该把你的职能分开。 使用资源生成的对象时,不需要传入id:

$scope.post = Post.get(id: 32723)
$scope.addBang = ->
  $scope.post.title = $scope.post.title + "!"
  $scope.post.$update()
对于问题1,考虑Rails表单。 如果您想避免传递受限参数,只需将它们保留在表单之外。 然而,如果您想使用Angular的内置模型/资源接口,那么说起来容易做起来难

因此,我建议创建一个Rails模型方法,该方法接受完整的params[:post]散列,并返回一个只有可访问方法的散列

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

编辑: 或者,更好的是,正如我刚刚学到的,在将散列提交给AngularJS之前,使用Rails模型生成散列

像这样:

post = Post.find(123)
post = { name: post.title, content: post.content }
或者是已经实现了RESTful架构并使用承诺的优秀软件。此外,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_可访问的东西,即使您没有使用rails 4,您也可能想看看


因为,如果要在发送post请求之前进行过滤,则必须编写params.require().permit()的javascript版本。或者可能我对这个问题不是很了解…?

不确定这是否能帮到你,但angular js上有一个很好的rails。不过这一集是“专业版”,所以你必须注册(9个月)