Search code examples
ruby-on-railsapidevise

Rails API - No route matches [POST]


I am experimenting the Rails API with devise. I am trying to create a POST request so that the user can autenticate using the email and password. To do so, I am using devise and simple token authentication

However, when I submit my POST request using postman, I get the error:

ActionController::RoutingError (No route matches [POST] "/v1/sessions"):

I think the issue is that is sending the post to: /v1/sessions rather than api/v1/sessions.

However, I do not understand why since I declared my routes such as: api-->v1-->sessions

Folder structure of controller

enter image description here

Routes

Rails.application.routes.draw do
  # devise_for :users
  namespace :api do
    namespace :v1 do
      resources :sessions, only: [:create, :destroy]
    end
  end
end

Controller

class V1::SessionsController < ApplicationController

  def create
    user = User.where(email: params[:email]).first
    if user&.valid_password?(params[:password])
      render json: user.as_json(only: [:email, :authentication_token]), status: :created
    else
      head(:unauthorized)
    end
  end

  def destroy

  end

end

Solution

  • shoud it be class Api::V1::SessionsController < ApplicationController instead?