Circular Dependency Error autoloading constant - ruby-on-rails

I keep getting the error "Circular dependency detected while autoloading constant API::V1::CitysController" when I try load my api page. Everything I've searched seems to suggest their might be a typo but I don't think there is one.
My routes:
namespace :api , defaults: {format: 'json'} do
namespace :v1 do
resources :citys
end
end
my controller is in app/controllers/api/v1/citys_controller.rb
Theres nothing in it really at the moment
class Api::V1::CitysController < ApplicationController
respond_to :json
def index
end
end
Not sure what else is relevant to the problem? It should just load a blank page without any errors when I go to localhost:3000/api/v1/citys
Added routes
Prefix Verb URI Pattern Controller#Action
pages_home GET /pages/home(.:format) pages#home
root GET / pages#home
api_v1_citys GET /api/v1/citys(.:format) api/v1/citys#index {:format=>"json"}
POST /api/v1/citys(.:format) api/v1/citys#create {:format=>"json"}
new_api_v1_city GET /api/v1/citys/new(.:format) api/v1/citys#new {:format=>"json"}
edit_api_v1_city GET /api/v1/citys/:id/edit(.:format) api/v1/citys#edit {:format=>"json"}
api_v1_city GET /api/v1/citys/:id(.:format) api/v1/citys#show {:format=>"json"}
PATCH /api/v1/citys/:id(.:format) api/v1/citys#update {:format=>"json"}
PUT /api/v1/citys/:id(.:format) api/v1/citys#update {:format=>"json"}
DELETE /api/v1/citys/:id(.:format) api/v1/citys#destroy {:format=>"json"}

I'm not sure why, but the error says it is looking for API::V1::CitysController class in your citys_controller.rb file and you have Api::V1::CitysController. So first and foremost change the name of your class to API::V1::CitysController (note the capitalized 'API'). That should solve your immediate problem.

Related

Routing Error No route matches [GET] "/index"

I'm new to Rails. I've got a routing error bellow when I try to access http://localhost:3000/index.
No route matches [GET] "/index"
Rails.root: /Users/[...]/taskleaf
Routes
Helper HTTP Verb Path Controller#Action
root_path GET / tasks#index
tasks_path GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
[...]
Here is the content of my route.rb file:
Rails.application.routes.draw do
root to: 'tasks#index'
resources :tasks
end
I would be very grateful if you could help me out ...
from what you've provided, try changing it to either '/' or '/tasks/index' then you may get what you're looking for.
Or, add this code in your routes.rb file get '/index', to: 'tasks#index', which would direct /index path to the index action in your tasks controller.
Here is how Rails routing works: when you request '/index', it'll try to find #index route in your routes.rb file, which doesn't exist since the index action lies within your 'tasks' controller (from what I've seen from your codes). In the code, you defined the root page ('/') to be '/tasks/index' (try visiting '/' and '/tasks/index', they should be the same).
Remember, whenever you've seen a routing error, it is likely that you didn't define the route in routes.rb file
Feel free to give it a try and update here if there's any further errors/problems.
Cheers :)

New and Edit Routes missing in Rails Resources routes

In building a Rails API, I declared my routes file as:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
root 'budgets#index'
resources :users do
resources :budgets
end
post '/login', to: 'sessions#create'
post '/logout', to: 'sessions#destroy'
resources :budgets do
resources :budget_totals
end
end
end
end
However, after running the rails routes command, I found that the "new" and "edit" routes are missing. How do I rectify this?
The relevant portion of the rails routes response is:
Prefix Verb URI Pattern Controller#Action
api_v1_root GET /api/v1(.:format) api/v1/budgets#index
api_v1_user_budgets GET /api/v1/users/:user_id/budgets(.:format) api/v1/budgets#index
POST /api/v1/users/:user_id/budgets(.:format) api/v1/budgets#create
api_v1_user_budget GET /api/v1/users/:user_id/budgets/:id(.:format) api/v1/budgets#show
PATCH /api/v1/users/:user_id/budgets/:id(.:format) api/v1/budgets#update
PUT /api/v1/users/:user_id/budgets/:id(.:format) api/v1/budgets#update
DELETE /api/v1/users/:user_id/budgets/:id(.:format) api/v1/budgets#destroy
api_v1_users GET /api/v1/users(.:format) api/v1/users#index
POST /api/v1/users(.:format) api/v1/users#create
api_v1_user GET /api/v1/users/:id(.:format) api/v1/users#show
PATCH /api/v1/users/:id(.:format) api/v1/users#update
PUT /api/v1/users/:id(.:format) api/v1/users#update
DELETE /api/v1/users/:id(.:format) api/v1/users#destroy
api_v1_login POST /api/v1/login(.:format) api/v1/sessions#create
api_v1_logout POST /api/v1/logout(.:format) api/v1/sessions#destroy
api_v1_budget_budget_totals GET /api/v1/budgets/:budget_id/budget_totals(.:format) api/v1/budget_totals#index
POST /api/v1/budgets/:budget_id/budget_totals(.:format) api/v1/budget_totals#create
api_v1_budget_budget_total GET /api/v1/budgets/:budget_id/budget_totals/:id(.:format) api/v1/budget_totals#show
PATCH /api/v1/budgets/:budget_id/budget_totals/:id(.:format) api/v1/budget_totals#update
PUT /api/v1/budgets/:budget_id/budget_totals/:id(.:format) api/v1/budget_totals#update
DELETE /api/v1/budgets/:budget_id/budget_totals/:id(.:format) api/v1/budget_totals#destroy
api_v1_budgets GET /api/v1/budgets(.:format) api/v1/budgets#index
POST /api/v1/budgets(.:format) api/v1/budgets#create
api_v1_budget GET /api/v1/budgets/:id(.:format) api/v1/budgets#show
PATCH /api/v1/budgets/:id(.:format) api/v1/budgets#update
PUT /api/v1/budgets/:id(.:format) api/v1/budgets#update
DELETE /api/v1/budgets/:id(.:format) api/v1/budgets#destroy
Are you perhaps using the api_only app? Those do not generate New and Edit routes.
Did you create your app with the --api switch, such as: rails new my-api --api? The --api argument tells Rails that you want an API application only.
See "What Is Rails API?" and "Rails: Building a Ruby on Rails API-only app".

Api rails: no route matches

I did a rails 5 api with Sourcey tutorial and now i get a routing error.
Curl command curl -H "Authorization: Token token=8NjQH4YVWQdUIve4xDQBaArr" http://localhost:3000/v1/users display a 404 not found message and 'no route matches' is raised in my term where the server is running
No route matches [GET] "/v1/users"
So i checked rails routes. The output is:
v1_users GET /v1/users(.:format) api/v1/users#index {:subdomain=>"api"}
POST /v1/users(.:format) api/v1/users#create {:subdomain=>"api"}
v1_user GET /v1/users/:id(.:format) api/v1/users#show {:subdomain=>"api"}
PATCH /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"}
PUT /v1/users/:id(.:format) api/v1/users#update {:subdomain=>"api"}
DELETE /v1/users/:id(.:format) api/v1/users#destroy {:subdomain=>"api"}
we notice we have GET /v1/users and /:id
How to solve this problem ?
routes.rb:
Rails.application.routes.draw do
constraints subdomain: 'api' do
scope module: 'api' do
namespace :v1 do
resources :users
end
end
end
end
Users_controller.rb and api_controller.rb are inside /app/controller/api/v1/.
If you need another part of the code (controllers, models ....) do not hesitate to ask
The problem is that your route has a subdomain constraint. So that your api would only be available at http://api.example.com/v1/users.
You cannot use subdomains with localhost without setting up a virtual host via apache or nginx, editing the hosts file or using a service like Ngrok.
You could also convert it to a path constraint instead:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
end
end
end

No route matches [POST] OmniAuth Steam Callback

I'm building Rails API for AngularJS app. I'm using devise_token_auth and omniauth-steam gems. When I try to authenticate user using the Steam there is an error:
ActionController::RoutingError (No route matches [POST] "/omniauth/steam/callback"
I've added devise_token_auth routes, but they don't create POST callbacks. I've tried to manually create POST route for callback, but it hasn't worked and I'm not sure if it is even correct solution. I'm trying to solve this problem from yesterday and I can't find anyone with the similar one.
config/routes.rb
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
mount_devise_token_auth_for 'Api::V1::User', at: 'auth'
end
end
end
config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :steam, ENV['steam_api_key']
end
I use figaro gem and save steam_api_key in application.yml file.
rake routes task
Prefix Verb URI Pattern Controller#Action
new_api_v1_api_v1_user_session GET /api/v1/auth/sign_in(.:format) devise_token_auth/sessions#new
api_v1_api_v1_user_session POST /api/v1/auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_api_v1_api_v1_user_session DELETE /api/v1/auth/sign_out(.:format) devise_token_auth/sessions#destroy
api_v1_api_v1_user_password POST /api/v1/auth/password(.:format) devise_token_auth/passwords#create
new_api_v1_api_v1_user_password GET /api/v1/auth/password/new(.:format) devise_token_auth/passwords#new
edit_api_v1_api_v1_user_password GET /api/v1/auth/password/edit(.:format) devise_token_auth/passwords#edit
PATCH /api/v1/auth/password(.:format) devise_token_auth/passwords#update
PUT /api/v1/auth/password(.:format) devise_token_auth/passwords#update
cancel_api_v1_api_v1_user_registration GET /api/v1/auth/cancel(.:format) devise_token_auth/registrations#cancel
api_v1_api_v1_user_registration POST /api/v1/auth(.:format) devise_token_auth/registrations#create
new_api_v1_api_v1_user_registration GET /api/v1/auth/sign_up(.:format) devise_token_auth/registrations#new
edit_api_v1_api_v1_user_registration GET /api/v1/auth/edit(.:format) devise_token_auth/registrations#edit
PATCH /api/v1/auth(.:format) devise_token_auth/registrations#update
PUT /api/v1/auth(.:format) devise_token_auth/registrations#update
DELETE /api/v1/auth(.:format) devise_token_auth/registrations#destroy
api_v1_api_v1_user_confirmation POST /api/v1/auth/confirmation(.:format) devise_token_auth/confirmations#create
new_api_v1_api_v1_user_confirmation GET /api/v1/auth/confirmation/new(.:format) devise_token_auth/confirmations#new
GET /api/v1/auth/confirmation(.:format) devise_token_auth/confirmations#show
api_v1_auth_validate_token GET /api/v1/auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
api_v1_auth_failure GET /api/v1/auth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /api/v1/auth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#omniauth_success
GET /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
omniauth_failure GET /omniauth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /api/v1/auth/:provider(.:format) redirect(301)
I know that it's a bit messy because of namespaces, but it shouldn't cause this problem, right?
EDIT:
I did some research and here is the link https://github.com/lynndylanhurley/devise_token_auth#usage-tldr it says that /:provider/callback url should have GET/POST action, but as we can see I don't have POST action for callback.
Finally I've solved this problem by adding below line to the routes.rb file.
post '/omniauth/steam/callback', to: 'overrides/omniauth_callbacks#redirect_callbacks'
I've created omniauth_callbacks_controller.rb file in controllers/overrides folder and removed below line.
skip_before_action :set_user_by_token, raise: false
The last step was editing line with redirect route. I've changed this:
redirect_route = "#{request.protocol}#{request.host_with_port}/#{Devise.mappings[devise_mapping].fullpath}/#{params[:provider]}/callback"
To hard-coded route.
redirect_route = "/api/v1/auth/steam/callback"

No route matches {:controller=>"comments", :action=>"create"} - It does

Edit: I've seen a number of these but couldn't find an answer to this so I'm attempting to document it as best I can and asking this question.
I have a model-less rails app (calling an API) with a nested comments resource. I am able to post a comment against a story if I go directly to the comments#new or comments#index action and accordingly post to the comments#create action.
However I'd like very much to be able to post a comment on the same page as the #show action of the parent resource: (opusses#show)
I've tried using the rails url_helper path from rake routes as opuss_comments_path and explicitly stating the controller and action. In both cases I still get this message:
No route matches {:controller=>"comments", :action=>"create"}
Here is my routes db:
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :osessions, only: [:new, :create, :destroy]
resources :authors do
member do
get :following
get :followed
post :follow
end
end
resources :opusses do
resources :comments
member do
get :like
get :authorfeed
post :repost
end
end
And my Rake Routes:
DELETE /authors/:id(.:format) authors#destroy
opuss_comments GET /opusses/:opuss_id/comments(.:format) comments#index
POST /opusses/:opuss_id/comments(.:format) comments#create
new_opuss_comment GET /opusses/:opuss_id/comments/new(.:format) comments#new
edit_opuss_comment GET /opusses/:opuss_id/comments/:id/edit(.:format) comments#edit
opuss_comment GET /opusses/:opuss_id/comments/:id(.:format) comments#show
PUT /opusses/:opuss_id/comments/:id(.:format) comments#update
DELETE /opusses/:opuss_id/comments/:id(.:format) comments#destroy
&&
like_opuss GET /opusses/:id/like(.:format) opusses#like
authorfeed_opuss GET /opusses/:id/authorfeed(.:format) opusses#authorfeed
repost_opuss POST /opusses/:id/repost(.:format) opusses#repost
opusses GET /opusses(.:format) opusses#index
POST /opusses(.:format) opusses#create
new_opuss GET /opusses/new(.:format) opusses#new
edit_opuss GET /opusses/:id/edit(.:format) opusses#edit
opuss GET /opusses/:id(.:format) opusses#show
PUT /opusses/:id(.:format) opusses#update
DELETE /opusses/:id(.:format) opusses#destroy
When I call the code below from comments#index page it works perfectly. However it's quite common to post to another form from a different controller and when I call this code from the opusses#show page it fails with the error above.
On the off chance it had to do with the URL helper, I tried specifying the controller and action explicitly and that still didn't work - generated the same error.
Classic newbie mistake, but for others benefit =>
I had rake routes and I had the path correct, what I wasn't doing was submitting a the id of the parent resource. So POST to the path and include the object in question. In my case this mean opuss_comments_path(#opuss["xyz"]) where xyz was the id of my object.
opuss_comments GET /opusses/:opuss_id/comments(.:format) comments#index
POST /opusses/:opuss_id/comments(.:format) comments#create
Ah.. learning. :)
Based on your routes, You shouldn't have to use a url helper. but you do have to make sure that you have a handle on the Opuss object in the controller. so do something like this ;
#opuss = Opuss.find(params[:id]) #or your equivalent finder code
#comment = #opuss.comments.build
and then in your view;
<%= form_for([#opuss, #comment]) do |f| %>
.... rest of form
<% end %>

Resources