Routing error in ruby rails - ruby-on-rails

So, I am trying to send a GET request from Postman and trying to run the method called question. I am also passing parameters in my request.
I keep getting a routing error. Im not sure why.
The way I am trying to route it is given below
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
get 'questions', to: 'application#/api/v1/question'
end
end
end
My questions_controller.rb has a method called question. The file is in app/controllers/api/v1/questions_controller.rb
The error I am getting,
Picture of the error I am getting

have you tried this?
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
get 'questions', to: 'questions#question'
end
end
end
Since you already said your namespaces maybe it isn't necessary

Related

uninitialized constant Api::Api in rails

I am trying to namespace my controllers in my rails app. I have in my controller :
#controllers/api/auth_controller.rb
class Api::AuthController < ApplicationController
def register
end
end
And in my route file i have:
namespace :api do
get "auth/register", to: "api/auth#register"
end
I keep on getting the error:
uninitialized constant Api::Api Object.const_get(camel_cased_word) ^^^^^^^^^^ raise MissingController.new(error.message, error.name) ^^^^^
What am i doing wrong?
It's giving you that error because you're telling rails to look for api/ inside of namspace :api so it's trying to find Api::Api, You just need to remove the api from api/auth#register, it looks for api automatically since the route is under namespace :api, just do get "auth/register", to: "auth#register" and you should be good.
More on namespaces in routing here

Rails Non Standard Resource Route

I'd like to create a route that's like this:
https://example.com/appusers/1/check_for_updates.
It should basically be a resourced route so that I can do #appuser = Appuser.find(params[:id])
But I can't figure out how to list that in my routes file.
I've tried get 'appusers/:id/check_for_updates' but that throws an error.
You can do it using :member
resources :appusers do
get :check_for_updates, on: :member
end
Rails documentation

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

Rails namespaced routes to wrong Controller

So I'm just beginning with RoR and figured I do a basic blog with API endpoints aswell. The problem is that my api requests seem to be routed to the wrong controller,
I have the following as my routes.rb
Blog::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles
end
end
end
I also have controllers/api/v1/articles_controller.rb, which has the following content:
module API
module V1
class ArticlesController < ApplicationController
respond_to :json
def index
respond_with Article.all
end
end
end
end
My logic says that when I hit http://localhost:3000/api/v1/articles, this should be the Controller to respond, however the actual Controller that responds is the one in the root of controllers (controllers/articles_controller.rb) and not the one in the /api/v1 path. When I remove the Controller that actually responds, I'll get uninitialized constant Api::V1::ArticlesController instead.
Even rake routes gives me the expected routes, however actually hitting those endpoints fails. Output of rake routes is the following:
api_v1_articles GET /api/v1/articles(.:format) api/v1/articles#index
POST /api/v1/articles(.:format) api/v1/articles#create
new_api_v1_article GET /api/v1/articles/new(.:format) api/v1/articles#new
edit_api_v1_article GET /api/v1/articles/:id/edit(.:format) api/v1/articles#edit
api_v1_article GET /api/v1/articles/:id(.:format) api/v1/articles#show
PUT /api/v1/articles/:id(.:format) api/v1/articles#update
DELETE /api/v1/articles/:id(.:format) api/v1/articles#destroy
The only similar question I found on SO is nested namespace route going to wrong controller however, there's no accepted answer there and it's been a year. Maybe another attempt will help resolve this issue
Your module is API, but Rails is looking for Api. Ruby's modules are case-sensitive.

Ruby on Rails : add a new route

I'm new with RoR so this is a newbie question:
if I have a controller users_controller.rb and I add a method foo, shouldn't it create this route?
http://www.localhost:3000/users/foo
because when I did that, I got this error:
Couldn't find User with id=foo
I of course added a view foo.html.erb
EDIT:
I added to routes.rb this code but I get the same error:
resources :users do
get "signup"
end
This doesn't work automatically in rails 3. You'll need to add
resource :users do
get "foo"
end
to your routes.rb
You'll definitely want to have a look at http://guides.rubyonrails.org/routing.html, it explains routing pretty well.
Rails is directing you to the show controller and thinks that you're providing foo as :id param to the show action.
You need to set a route that will be dispatched prior to being matched as /users/:id in users#show
You can accomplish this by modifying config/routes.rb by adding the following to replace your existing resource describing :users
resource :users do
get "foo"
end
Just to add to the other answers, in earlier versions of Rails there used to be a default route
match ':controller(/:action(/:id))(.:format)'
which gave the behaviour you describe where a request of the form controller/action would call the given method on the given controller. This line is still in routes.rb but is commented out by default. You can uncomment it to enable this behaviour but the comment above it explains why this is not recommended:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
At the schema ':controller/:action(.:format)', you can also easily do the following
resources :users do
get "foo", on: :collection
end
or
resources :users do
collection do
get 'foo'
end
end
http://guides.rubyonrails.org/routing.html#adding-collection-routes

Resources