@k12da (K Yoshida)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

devise 新規登録時のルーティングエラー

解決したいこと

gemのdeviseを使って新規登録を行いたいのですが、ルーティングエラーが発生します。
※APIモードでの実装前提です。

発生している問題・エラー

 "status": 404,
    "error": "Not Found",
    "exception": "#<ActionController::RoutingError: No route matches [POST] \"/users/registrations\">",

スクリーンショット 2022-12-14 5.49.21(2).png

該当するソースコード

config/routes.rb
Rails.application.routes.draw do

  devise_for :users, controllers: { :registrations => "users/registrations" }
  
  devise_scope :user do
    get "user/:id", :to => "users/registrations#detail"
    get "signup", :to => "users/registrations#new"
    get "login", :to => "users/sessions#new"
    get "logout", :to => "users/sessions#destroy"
  end

  namespace 'api' do
    namespace 'v1' do
      namespace 'clients' do
        namespace 'admin' do
          # resources :users, defaults: { format: :json } do
          # end
        end
        resources :users, defaults: { format: :json } do
        end
      end
    end
  end
end
app/controllers/application_controller.rb
class ApplicationController < ActionController::API

    before_action :configure_permitted_parameters, if: :devise_controller?

    protected
    def configure_permitted_parameters
        devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
        devise_parameter_sanitizer.permit(:account_update, keys: [:name])
    end
end

app/controllers/api/v1/clients/users/registrations_controller.rb
# frozen_string_literal: true

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  def create
    super
    @user = User.new(user_params)
    @user.save
  end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
  end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end

  private

  def user_params
    params.permit(:user, keys: [:name, :email, :password])
  end
end

自分で試したこと

APIモードでの作成のため、URLは以下の通りなのですが、api/v1/clients/を消したりしても変わりませんでした。
以下のの指定が間違っているのでしょうか?
http://localhost:3000/api/v1/clients/users/registrations

ちなみにrails routesですと、以下のようになっています。(POSTではなくGETになっていますが。。)

new_api_v1_clients_user_registration GET    /api/v1/clients/users/sign_up(.:format)                                                           registrations#new
users/registrations#detailsignup GET    /signup(.:format)                                                                                 users/registrations#new


以下の記事を参考にしました。
https://easyramble.com/cutomize-controllers-on-rails-devise.html
https://qiita.com/krile136/items/e04181bb9e9dc0f37a79

0 likes

No Answers yet.

Your answer might help someone💌