Certain namespaced routes being ignored - ruby-on-rails

I have the following routes
resources :eclubs, except: [:show]
namespace :eclubs do
resources :leaders, only: [:index, :show, :new, :create, :destroy]
resources :members, only: [:index, :show, :new, :create, :destroy]
end
However, /eclubs/members does not routes to the index action of the Eclubs::Members controller. Instead it routes to the show action of the Eclubs controller. How do I fix this?

Because it is declared first, the Eclubs controller is being given precedence over the Eclubs::Members. Instead declare the Eclubs controller's routes last e.g.
namespace :eclubs do
resources :leaders, only: [:index, :show, :new, :create, :destroy]
resources :members, only: [:index, :show, :new, :create, :destroy]
end
resources :eclubs, except: [:show]

Related

Rails Routes for API

I am trying to create the following Routes
/plans
/plans/:plan_id/notes
/plans/:plan_id/notes/:id
/notes/:note_id/reply
/notes/:note_id/upvote
This is my Route file
resources :plans, only: [:create, :index, :show] do
resource :note, only: [:create]
resources :notes, only: [:destroy]
end
resources :notes, only: [:index] do
resource :note_replies, only: [:create, :destroy]
resources :note_upvotes, only: [:create, :destroy]
end
Is there a way to remove duplication of routes created for notes?

Rails combine member & collection in routes.rb

I am trying to combine collection and member in the conversations path. But I could not figure it out,
resources :conversations, only: [:index, :show, :destroy] do
member do
post :reply
post :restore
end
end
and;
resources :conversations, only: [:index, :show, :destroy] do
collection do
delete :empty_trash
end
end
When I combine them it does not work, and obviously this one is wrong too!.
Combine member and collection in resources block. Like this,
resources :conversations, only: [:index, :show, :destroy] do
member do
post :reply
post :restore
end
collection do
delete :empty_trash
end
end
Or you may combine it like this also,
resources :conversations, only: [:index, :show, :destroy] do
post :reply, on: :member
post :restore, on: :member
delete :empty_trash, on: :collection
end
try this
resources :conversations, only: [:index, :show, :destroy] do
member {
post :reply
post :restore
}
collection {
delete :empty_trash
}
end

uninitialized constant UsersController

I am using devise and therefore do not need a users controller.However, i also need nested routes and my config.routes looks like this;
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
resources :users do
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end
resources :businesses do
resources :business_accounts, path: "business_account", only: [:show] do
resources :business_withdraws, only: [:new, :show, :create, :index]
resources :business_deposits, only: [:new, :show, :create, :index]
end
end
end
How can i go past this error while also maintaining my nested routes.
Thank you.
You have three levels of nested routes there, which is normally considered to be undesirable: http://edgeguides.rubyonrails.org/routing.html#nested-resources
Resources should never be nested more than 1 level deep.
This bit resources :users do will create all the named routes for the users controller, which I suspect is where your error comes from. Why do you need this? Better perhaps to specify the routes without it?
resources :personal_accounts,path: "user_account", only: [:show] do
resources :deposits, only: [:new, :show, :create, :index]
resources :withdraws, only: [:new, :show, :create, :index]
end

Refactor nested routes for common default

I have some routes for an API which all have the same defaults (format: :json):
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :update], defaults: { format: :json }
resources :items, only: [:index, :show, :update, :destroy], defaults: { format: :json }
resources :posts, only: [:index, :show, :update], defaults: { format: :json }
resources :comments, only: [:index, :show, :update], defaults: { format: :json }
resources :flags, only: [:index, :show, :update, :create], defaults: { format: :json }
end
end
Is there a way to refactor/DRY the code to set the defaults (or even the only) in just one place for only this set of routes? The app also serves HTML at other routes, so it can't be a blanket setting for the whole app.
Move defaults: {format: :json} and the common only options at namespace level. Namespace have them as an option.
namespace :api, defaults: { format: :json }, only: [:index, :show, :update] do
namespace :v1 do
resources :users
resources :items, only: [:index, :show, :update, :destroy]
resources :posts
resources :comments
resources :flags, only: [:index, :show, :update, :create]
end
end
You can use iterator like the following:
[:users, :items, :posts, :comments, :flags].each do |res|
resources res, only:[...], defaults: {}
end
but I see you have different only so you can also pass it to iterator

Getting ActionController::RoutingError uninitialized constant while trying to use namespace in Rails routes

I have two controllers dashboard and posts. Im trying to put the post's :new, :create, :destroy, :edit, :update actions within dashboard url like this dashboard/posts/new. But the im talking to the new action in the posts controller. Not in the dash controller. Here's my routes.rb file :
resources :posts, :except => [:new, :create, :destroy, :edit, :update]
get 'dashboard', to: 'dash#show'
namespace :dashboard do
resources :posts, :only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The two controllers in question are : dash and posts
Now when I try to visit http://localhost:3000/dashboard/posts/new it says :
ActionController::RoutingError at /dashboard/posts/new
uninitialized constant Dashboard
How to fix this?
Assuming that you have two controllers DashController and PostsController.
And you want to access few of the posts routes within the scope of namespace then you can define the routes as below:
resources :posts, :only => [:index, :show]
get 'dashboard', to: 'dash#show'
scope :dashboard do
resources :posts, controller: :posts ,:only => [:new, :create, :destroy, :edit, :update]
end
get 'dashboard/posts', to: 'dash#posts'
The constant "Dashboard" can't be found. I think the problem is that you sometimes use dash and sometimes use dashboard.

Resources