ruby-on-rails Ruby on Rails对所有路由响应“no route matches”,而rack routes命令按预期显示它们

h7wcgrx3  于 2023-05-23  发布在  Ruby
关注(0)|答案(1)|浏览(168)

情况

我是Ruby的新手,但对Django,Node和其他技术有一定的经验。我正试图在本地运行一个来自雇主的遗留项目,该项目实际上正在生产环境中运行。我做的第一件事就是把它对接起来。Puma服务器启动良好,它连接到postgres数据库,响应ok到root 127.0.0.1:3000,并带有Rails默认的欢迎页面。甚至swagger显示了预期的端点,甚至/rails/info/routes也完美地显示了路由。也耙路由显示完全相同:

user@local:~/path_to/project$ sudo docker compose exec app rake routes
                               Prefix Verb   URI Pattern                                                                              Controller#Action                                                                 api/v1/users#index
                                      POST   /api/users(.:format)                                                                     api/v1/users#create
                             api_user GET    /api/users/:id(.:format)                                                                 api/v1/users#show
                                      PATCH  /api/users/:id(.:format)                                                                 api/v1/users#update
                                      PUT    /api/users/:id(.:format)                                                                 api/v1/users#update
                                      DELETE /api/users/:id(.:format)                                                                 api/v1/users#destroy
                       api_auth_login POST   /api/auth/login(.:format)                                                                api/v1/authentication#login

但是当我向/API/auth/login发出请求时,它会响应:

{
    "status": 404,
    "error": "Not Found",
    "exception": "#<ActionController::RoutingError: No route matches [POST] \"/api/auth/login\">",
    "traces": {
        "Application Trace": [],
        "Framework Trace": [
            {
                "exception_object_id": 47259696989680,
                "id": 0,
                "trace": "actionpack (6.0.3.7) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'"
            },
            {
                "exception_object_id": 47259696989680,
                "id": 1,
                "trace": "actionpack (6.0.3.7) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'"
            }, 
[...]

控制器

├── controllers
│   ├── api
│   │   └── v1
│   │       ├── api_controller.rb
│   │       ├── authentication_controller.rb
│   │       ├── axes_controller.rb
│   │       ├── invites_controller.rb
│   │       ├── metrics_controller.rb
│   │       ├── organizations_controller.rb
│   │       └── users_controller.rb
│   └── application_controller.rb

authentication_controller.rb

module Api::V1
  class AuthenticationController < ApiController
    before_action :authorize_request, except: :login
    include AuthHelper
    def login
      @user = User.find_by(game_id: params[:game_id], email: params[:email])
      if @user&& @user.valid_password?(params[:password])
        render json: generateToken(@user), status: :ok
      else
        render json: { error: 'unauthorized' }, status: :unauthorized
      end
    end

    private

    def login_params
      params.permit(:email, :password, :game_id)
    end
   
  end
end

config/routes.rb

Rails.application.routes.draw do
  
  mount Rswag::Ui::Engine => '/api-docs'
  mount Rswag::Api::Engine => '/api-docs'
  devise_for :users
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
  namespace :api do
    scope module: :v1, constraints: ApiVersion.new('v1') do

      resources :axes
      get '/metrics', to: 'metrics#get_metrics'
      get '/requests', to: 'organizations#requests'
      resources :organizations
      resources :invites
      resources :users do 
        post 'forgot_password', on: :collection
        post 'reset_password', on: :collection
        post 'google_auth', on: :collection
        get 'check_hash', on: :collection
        get 'export', on: :collection
      end
      post '/auth/login', to: 'authentication#login'
      post '/addmember', to: 'users#addUserOrganization'
      get '/*a', to: 'application#not_found'
    end
  end
end

我不知道还能做什么。请帮帮忙!!

neekobn8

neekobn81#

我没有意识到约束,现在寻找ApiVersion类...

class ApiVersion
  attr_reader :version, :default

   def initialize(version, default = false)
    @version = version
    @default = default
  end

   # check whether version is specified or is default
  def matches?(request)
    check_headers(request.headers) || default
  end

   private

   def check_headers(headers)

    accept = headers[:accept]
    accept && accept.include?("application/vnd.some_string.#{version}+json")
  end
end

所以添加标题

Accept: "application/vnd.some_string.v1+json"

成功了

相关问题