【问题标题】:getting results of custom action in rails with angularjs使用angularjs在rails中获取自定义操作的结果
【发布时间】:2013-03-21 07:11:33
【问题描述】:

我正在使用带有 angularjs 的 rails 3.2,我正在尝试了解如何使用 angular 的 $resource。

我希望资源从非默认操作返回结果(因此不获取、保存、查询、删除和删除)

所以在我的帖子控制器中我有

def home
  @recent_posts = Post.recent_posts
  respond_to do |format|
    format.html
    format.json
  end
end

然后在我的 js 文件中

app.factory "Post", ["$resource", ($resource) ->
  $resource("/post/:id/:action", {id: "@id"}, {home: {method: "GET", isArray: true}})
]

@HomeCtrl = ["$scope", "Post", ($scope, Post) ->
  $scope.recent_posts = Post.home()
]

在我看来,如果我尝试执行ng-repeat="post in recent_posts" 之类的操作,它不会返回任何内容。

【问题讨论】:

  • $scope.recent_posts = Post.home({action:'home', function(response){console.log(response) }, function(response){console.log(response)} })。请注册这些回调并检查响应中的内容。第一个函数是成功回调,第二个是错误回调。

标签: ruby-on-rails angularjs


【解决方案1】:

您忘记指定action 参数:

app.factory "Post", ["$resource", ($resource) ->
  $resource "/post/:id/:action",
    id: "@id"
  ,
    home:
      method: "GET"
      params:
        action: "home"

      isArray: true

]

【讨论】:

    【解决方案2】:

    感谢您的建议,它们帮助我找到了最终可行的方法。我只是像这样更改了 Home Controller:

    @HomeCtrl = ["$scope", "Post", ($scope, Post) ->
      $scope.recent_posts = Post.home({action:'home'});
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多