hutusi
hutusi

Reputation: 437

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?

Upvotes: 0

Views: 76

Answers (1)

vee
vee

Reputation: 38645

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.

Upvotes: 1

Related Questions