Rails - 未经授权的错误

时间:2017-01-28 19:48:41

标签: ruby-on-rails

使用Rails 5和这种依赖关系:

gem 'active_model_serializers', '~> 0.10.0'

gem 'devise', github: 'plataformatec/devise', branch: 'master'
gem 'devise_token_auth', github: 'lynndylanhurley/devise_token_auth', branch: 'master'

我创建了一个控制器:

module API::V1
  class CommunityPeopleController < AuthenticatedAPIController

有两种方法:

# POST /community-people
def create

# DELETE /community-people/1
def delete

并在路线上配置:

namespace :api, defaults: { format: 'json' } do
    namespace :v1 do
      resources :communities

      post 'community-people',
          to: 'community_people#create'

      delete 'community-people/:id_community',
          to: 'community_people#delete'

    end
  end

但是当我执行POST时,我得到401错误状态。这在Rails控制台中显示:

Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 5ms (Views: 4.0ms | ActiveRecord: 0.0ms)

但DELETE工作正常。 PS:我已经检查了我发送给服务器的所有数据和参数。这完全没问题。用户已经过身份验证。

我该如何解决这个问题?

PS:更多信息: 检查AuthenticatedPIController

module API::V1
  class AuthenticatedAPIController < APIController
    before_action :authenticate_user!
  end
end

APIController只是扩展了ApplicationController:

  class APIController < ::ApplicationController

最后:

class ApplicationController < ActionController::API

由于

编辑我已经做了一些测试...如果我创建一个GET,那么auth没有问题。问题出在POST方法上。

  // Routes
  get 'community-people/test',
      to: 'community_people#test'

// Controller
def test
  puts 'got here!'
end

它有效。我尝试添加另一个存根POST操作但仍然无法工作:(

0 个答案:

没有答案