How to setup routes in subfolder? rails - ruby-on-rails

I'm really struggling how to setup controller which are placed in subfolder? 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.
Error in my console:
'Api/Auth/register' is not a supported controller name. 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
Here's inside of my 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:
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). 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

Related

Undefined method `respond_to' for Api::V1::Controller

I already went through the topics here but the solutions mentioned didn't work out for me.
I want to create an API for my rails application and followed the tutorial from Railscast for API versioning. I am using Rails 5.
This is my routes.rb file:
Rails.application.routes.draw do
namespace :api, defaults: { format: 'json' } do
namespace :v1 do
resources :orders
end
end
end
And this the neworders_controller.rb under controllers/api/v1:
module Api
module V1
class OrdersController < ApplicationController
respond_to :json
def index
respond_with Order.all
end
end
end
end
When visiting http://localhost:3000/api/v1/orders I always get the following exception:
undefined method `respond_to' for Api::V1::OrdersController:Class Did you mean? respond_to?
I have already added responder to my gemfile but that didn't work out.
In your orders controller, you made a spelling mistake. Change
repsond_with Order.all
to
respond_with Order.all
Also, you made a spelling mistake in your routes file too. Change:
namespace :api, defaults: { format: 'josn' } do
to
namespace :api, defaults: { format: 'json' } do

Rails resource route under namespace leading to "not a supported controller name."

I'm building an API in Rails and I'd like for it to be versioned. My routes.rb file consists of:
Rails.application.routes.draw do
namespace :V1 do
resources :users
end
end
And I have my controller under /app/controllers/V1/users_controller.rb, which has this content:
module V1
class UsersController < ApplicationController
def index
render json: {message: "This is a test!"}
end
end
end
When I try to run the rails server on the command line I get the following error:
`default_controller_and_action': 'V1/users' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
I've looked at the link the error message gave me, however it seems to be how to specify a controller to use with a resource. Rather than doing that, can Rails not automatically determine it from my directory structure?
try this one:
Rails.application.routes.draw do
namespace :v1 do
resources :users
end
end

Ruby on Rails route to controller not working

Attempting to create an API using doorkeeper. When I sign into my account and the session user is authenticated and access my API on
http://localhost:3000/api/v1/trials
I get a routing error page saying "uninitialized constant TrialsController" and a rake routes listing. Including:
trials_path GET /api/v1/trials(.:format) trials#index
I am using rails version 4.0.3, ruby 2.0.0. This is my config/routes.rb file:
MyApp::Application.routes.draw do
devise_for :users
use_doorkeeper :scope => 'oauth2' do
end
root :to => 'welcome#index'
scope 'api' do
scope 'v1' do
resources :trials
end
end
end
My app/ dir contains:
app/
controllers/
application_controller.rb
welcome_controller.rb
api/
v1/
trials_controller.rb
My trials_controller.rb is:
module Api::V1
class TrialsController < ::ApplicationController
doorkeeper_for :all
def index
#trials = Trials.all
end
def show
...
end
...
end
end
UPDATE:
When I change the routes.rb to namespace the trails controller like so:
namespace :api do
namespace :v1 do
resources :trails
end
end
I get a "no route matches" error when attempting to access:
http://localhost:3000/api/v1/trials(.json)
(With or without the extension.)
I have also added to trials#index:
def index
#trials = Trials.all
respond_to do |format|
format.json { render :json => #trials }
format.xml { render :xml => #trials }
end
end
Also with no luck.
I'm not sure how it would play out against doorkeeper but I have a similar API structure in an app. I have the following in my routes (matching what Sergio notes)
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :api_controller_1
resources :api_controller_2
end
end
And here's how I build my API classes:
module Api
module V1
class ApiNamedController < ApplicationController
# code
end
end
end

Rails and Devise - Namespace is Uninitialised Constant

I am trying to use namespaces to declare an api.
My routes.rb contains:
devise_scope :user do
namespace :api do
namespace :v1 do
match 'log_in', :to => 'token_authentications#log_in', :via => "post"
end
end
end
And my *token_authentications_controller.rb* looks like this:
class Api::V1::TokenAuthenticationsController < ApplicationController
...
def log_in
...
end
...
end
When I hit: api/v1/log_in I get:
Routing Error
uninitialized constant Api
So do I need to declare the namespace somewhere?
Rails expects namespaces to follow directory structure, unless I'm mistaken.
Given your class name for your controller, Api::V1::TokenAuthenticationsController, rails expects it to live in app/controllers/api/v1/token_authentications_controller.rb.
If you just move your controller to the correct folder, I think you should be fine.
You might also want to make sure to actually declare the namespace modules somewhere, like for instance refactoring your controller as such:
module Api
module V1
class TokenAuthenticationsController
...
end
end
end

rails 3 namespace resources routing exception

I'm building JSON api controllers.
my routes.rb has:
namespace :api do
resources :users
end
controllers/api/users.rb:
respond_to :json
def create
#user = User.create(params[:user])
respond_with(#user)
end
when posting to api/users.json a new user is created but I'm getting an exception saying user_url method is missing.
if i add: resources :users to routes.rb everything is fine.
what's happening? any other way to solve this ?
I think respond_with(#user) will redirect to user url but there is no users path declared outside namespace :api, so it alerts that error.
Could you try this?
respond_with(#user, :location => your_path_that_will_be_redirected)

Resources