Rails says No route matches but I see it in `rake routes` - ruby-on-rails

Rails says:
No route matches {:action=>"import", :controller=>"admin/users"}
but rake routes shows:
import_admin_user POST /admin/users/:id/import(.:format) admin/users#import
My routes.rb looks like:
namespace :admin do
resources :users do
member do
post :import
end
end
end
What am I missing? Rails 5.0.7

I got this working with:
namespace :admin do
resources :users do
collection do
post :import
end
end
end
Apparently collection is for actions that don't take an id.

Related

Ruby on Rails No route matches [POST]

I'm attempting to maintain some legacy code but I'm struggling to figure out why my defined route isn't working.
The error I'm getting is
Started POST "/api/transactions/weight" ...
ActionController::RoutingError (No route matches [POST] "/api/transactions/weight"):
My (simplified) route is as follows
namespace :api do
resources :transactions, only: [:create] do
post "weight", to: "transactions#weight"
end
end
I've also tried moving the route outside the resource :transactions definition like so
namespace :api do
resources :transactions, only: [:create]
post "transactions/weight", to: "transactions#weight"
end
But I'm getting the same error. Am I misunderstanding route definition, or is the problem elsewhere? Thanks
Always check bundle exec rake routes to understand your routes.
The post method defines "/weight" route as a member route (acting on a specific "transactions" resource
# bundle exec rake routes
api_transaction_weight POST /api/transactions/:transaction_id/weight(.:format). api/transactions#weight
By default, if you don't specify on: option, the route inside resources block gets created as member route.
You want to specify it as a collection route [1] via on: :collection
namespace :api do
resources :transactions, only: [:create] do
post "weight", to: "transactions#weight", on: :collection
end
end
# bundle exec rake routes
weight_api_transactions POST /api/transactions/weight(.:format) api/transactions#weight
[1] https://guides.rubyonrails.org/routing.html#adding-collection-routes
The way you defined routes will consider "weight" as the member route. If you want to define it as collection route.
namespace :api do
resources :transactions, only: [:create] do
collection {post :weight}
end
end
The above convention will expose weight as collection route and not member route.If you want to be defined as member post request. Then use following convention
namespace :api do
resources :transactions, only: [:create] do
member {post :weight}
end
end
If you are using api.rb as draw file in routes, then you need to restart your application to reflect changes in routes file.

need help on rails nesting routes

I want to get a route like
GET /example/notifications/status, to: example/notification#status
POST /example/notifications/disable to: example/notification#disable
Here is what I did
resources :example, only => %i[index] do
collection do
resources :notifications, :only => [] do
collection do
get :status
post :disable
end
end
end
end
it get the right path but it point to notification#status not example/notification#status
is there any way I can get both right path and controller expect code like
get "notifications/status", :to => "example/notifications#status"
You can use namespace for this purpose like below,
in you config/routes.rb file
namespace :example do
collection do
get :status
post :disable
end
end
It will hit the example/notification#status action.
for more information see here
Don't use resources since you don't want (aparently) any param on your routes. Go for namespace instead:
namespace :example do
namespace :notifications do
get 'status'
post 'disable'
end
end
Gives you:
$ rails routes -g example
Prefix Verb URI Pattern Controller#Action
example_notifications_status GET /example/notifications/status(.:format) example/notifications#status
example_notifications_disable POST /example/notifications/disable(.:format) example/notifications#disable

Rails 4: routing error under a namespace

In routes.rb:
namespace :account do
resource :groups
resource :posts
end
But I got error when located http://0.0.0.0:3000/account/groups :
***Unknown action
The action 'show' could not be found for Account::GroupsController***
I checked http://0.0.0.0:3000/rails/info/routes and got:
```
account_groups_path POST /account/groups(.:format) account/groups#create
new_account_groups_path GET /account/groups/new(.:format) account/groups#new
edit_account_groups_path GET /account/groups/edit(.:format) account/groups#edit
GET /account/groups(.:format) account/groups#show
PATCH /account/groups(.:format) account/groups#update
PUT /account/groups(.:format) account/groups#update
DELETE /account/groups(.:format) account/groups#destroy
```
Why account/groups not map to index method?
If you'd like to generate only index action then update your route file to restrict the routes created to just index as follows:
namespace :account do
resource :groups, only: [ :index ]
resource :posts
end
The problem however is your resource declaration, you're using singular resource, i.e. resource :groups and resource :posts. What this does is map /account/groups to account/groups#show and account/posts#show. Pluralizing resource will be a better solution:
namespace :account do
resources :groups
resources :posts
end
With this change, running rake routes should provide you the index action you are looking for.

Rails routing with subfolders: namespaces and resources

I have such route:
namespace admin
namespace :catalogs do
namespace :to do
resources :manufacturers do
resources :models do
resources :types do
resources :details
end
end
end
end
end
end
(please do not ask what's the model, why so man sub's: it must be so)
and such folder structure:
in manufacturer index view i have link:
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(c), :class=>'btn btn-primary'
(also rake routes say that it is right route)
and when i try to render my index view i get:
No route matches {:action=>"show",
:controller=>"admin/catalogs/to/models",
:manufacturer_id=>#}
when i custom my link to :
= link_to "Подробнее", admin_catalogs_to_manufacturer_model_path(manufacturer_id: c.MFA_ID, id: c.MFA_ID)
i get error
uninitialized constant Admin::Catalogs::To::ModelsController
what is wrong? how to solve my routing?

How to route an additional action for a namespaced resource?

In my routes.rb I have this to add "settings" as an additional action to insurances:
namespace :modules do
namespace :insurance do
resources :insurances do
member do
get :settings
end
end
end
end
According to "rake routes" this gives the following path:
settings_modules_insurance_insurance_path
But when I visit that path in the browser, it returns the error:
No route matches {:action=>"settings", :controller=>"modules/insurance/insurances"}
This is the full ouput of rake routes:
settings_modules_insurance_insurance GET /modules/insurance/insurances/:id/settings(.:format) {:action=>"settings", :controller=>"modules/insurance/insurances"}
What should I do?
You've put the new route on member, so you have to pass the id of the insurance:
settings_modules_insurance_insurance_path(#insurance)
The full name of the controller should be Modules::Insurance::InsurancesController, right?

Resources