简体   繁体   English

如何在子文件夹中设置路线? 导轨

[英]How to setup routes in subfolder? rails

I'm really struggling how to setup controller which are placed in subfolder?我真的很挣扎如何设置放在子文件夹中的 controller? I already tried below but I got an error in my console.我已经在下面尝试过,但我的控制台出现错误。 Somebody helped how to achieve and fix this, I'm from laravel and I using rails now.有人帮助如何实现和解决这个问题,我来自 laravel,我现在使用导轨。

Error in my console:我的控制台中的错误:

'Api/Auth/register' is not a supported controller name. 'Api/Auth/register' 不是受支持的 controller 名称。 This can lead to potential routing problems.这可能导致潜在的路由问题。

My target route is this below:我的目标路线如下:

http://localhost:3000/api/auth/register

Below image is directory where I place the register_controller下图是我放置register_controller的目录

在此处输入图像描述

Here's inside of my routes.rb这是我的routes.rb里面

Rails.application.routes.draw do
  namespace 'Api' do
    namespace 'Auth' do
      get 'register', to: 'register#store'
    end
  end
end

And in my register_controller.rb is below:在我的register_controller.rb中如下:

module Api
  module Auth
    class RegisterController < ApplicationController
      def store
        render json: { code: 200, data: 'sample' }, status: :ok
      end
    end
  end
end

You need to setup a namespace for your controller, and then a route for each of your actions (methods in your controllers).您需要为您的 controller 设置一个命名空间,然后为您的每个操作(控制器中的方法)设置一个路由。 Also, the namespace is usually lower case.此外,命名空间通常是小写的。

Rails.application.routes.draw do
  namespace 'api' do
    namespace 'auth' do
      namespace 'register' do
        get 'store'
      end
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM