I can't pass the rails well.

Asked 2 years ago, Updated 2 years ago, 44 views

railsg controller api/hello --no-assets --no-helper I ran this command to create an api for testing, but I can't pass it well

hello_controller.rb

 class Api::HelloController<ApplicationController

  def show# If you change this to index and write namespace resources:hello,only:[:index], it works as expected.
    render json: "Hello"
  end
end

routes.rb
Is it possible to pass through the path in the following way...

Rails.application.routes.drawdo
  get 'api/hello', to: 'hello#show'
  # or
  # get'password_resets/new' Because there was something going through the pass like this
  get 'api/hello/show'
end

If you access http://localhost:8888/api/hello in this path setting get'api/hello', to:'hello#show', you will see uninitialized constantHelloController.

I don't understand why I can't directly apply GET, POST methods, etc. to the method using resource.
Could you tell me the details?

ruby-on-rails ruby

2022-09-30 19:33

2 Answers

Rails routing allows you to create a namespace on the controller using namespace or scope.I think I can solve this problem by using this feature.

https://railsguides.jp/routing.html#%E3%82%B3%E3%83%B3%E3%83%88%E3%83%AD%E3%83%BC%E3%83%A9%E3%81%AE%E5%90%8D%E5%89%8D%E7%A9%BA%E9%96%93%E3%81%A8%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0


2022-09-30 19:33

get'api/hello', to:'api/hello#show' to pass the path.

get Set the actual path to throw access/hello, to:'Specify where the controller is located If hello_controller.rb is in the api folder, the above setting is set, and #show to access the function show in the file.'


2022-09-30 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.