【问题标题】:Rails to Angular: how to add act_as_followerRails 到 Angular:如何添加 act_as_follower
【发布时间】:2015-12-04 20:12:05
【问题描述】:

我的应用曾经是一个传统的 Rails 应用,包括 acts_as_follower gem。在 Rails 下,我会使用这样的代码:

    <% if current_user.following?(sentence) %>
        <%= link_to "Unfollow", unfollow_sentence_path(sentence) %>
    <% else %>
        <%= link_to "Follow", follow_sentence_path(sentence) %>
    <% end %>

现在我有了一个 Angular 前端,我想知道如何复制这个功能。我可以使用一堆 $http 调用,但这似乎不是正确的方法。

有什么建议吗?

【问题讨论】:

    标签: ruby-on-rails angularjs


    【解决方案1】:

    由于您可能使用 Rails 作为 API 后端,处理 Ajax 调用的最佳方法是使用 Angular Rails Resource 将它们包装在客户端(Angular)端

    这是一个简单的 Angular rails 资源服务:

    angular.module('mymodule').factory('Book', ['railsResourceFactory', 
      function (railsResourceFactory) {
        return railsResourceFactory({
          url: '/books',
          name: 'book'
        });
      }
    ]);

    在控制器中,你可以这样使用它:

    // Find all books matching the title
    Book.query({ title: title }).then(function (results) {
      $scope.books = results;
    }, function (error) {
      // do something about the error
      $scope.searching = false;
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多