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?
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.
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.'
© 2024 OneMinuteCode. All rights reserved.