簡體   English   中英

AngularJS和Rails其余設置

[英]AngularJS and Rails rest setup

AngularJS的新手。 使RESTful組件在角度和導軌之間工作有些麻煩。 我設置了一個簡單的休息服務並得到了罰款。 綁定工作等。但是當我$ 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

對於問題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 Model方法,該方法接受完整的params [:post]哈希並返回僅可訪問方法的哈希。

通過這樣做,您可以使其保持干燥狀態,並避免在模型更改時調整JS。

編輯:或者,更好的是,正如我剛剛學到的那樣,在將哈希傳遞給AngularJS之前,使用Rails模型生成哈希。

像這樣:

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

或者有一個出色的rails-resource-angular寶石 ,它已經實現了RESTful架構並使用了promises。 此外,發布請求將被正確包裝,因此您可以使用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,您也可能想看一下這篇文章

因為,如果您要在發送發布請求之前進行過濾,則必須編寫javascript版本的params.require()。permit()。 還是我對這個問題不太了解...?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM