Angular 和 Devise 权限错误

Angular and Devise permission error

我正在为我的网络应用程序使用 Angular 和 Devise,但我遇到了一个小问题。我不希望用户在未登录的情况下访问应用程序的某些部分,因此我在 application_controller.rb.

中使用 before_action :authenticate_user!

我使用 router-ui 在用户访问该页面时将他们路由到#home,http://localhost:3000/users/sign_in#/home

$stateProvider
  .state('home', {
    url: '/home',
    templateUrl: '../assets/angular-app/templates/_home.html.haml',
    controller: 'mainCtrl',
    resolve: {
      postPromise: ['posts', function(posts){
        return posts.getAll();
      }]
    }
  })

解析从我的数据库中检索所有帖子并将其输出到主页上。但是,当用户未登录时,JSON 文件给出 401 授权失败,因为他未登录。

解决此问题的最佳方法是什么?

在控制器中检索帖子时,您应该跳过 authenticate_user! 过滤器来响应请求的操作。

class YourController < ApplicationController

  skip_before_action :authenticate_user!, only: :your_action

  def your_action
    # the action which retrieves posts
  end
  ...
end