Domanda

I have a rails app with a route that is:

  resources :users do
    resources :api_keys
  end

now I have the controllers and models and all tests are passing - but I would like a signed in user to go to their personal development centre, so I created:

  resources :users do
    resources :api_keys
    resources :development_center
  end

The issue is, that while here I would like the user to be able to access methods from the api_keys controller, that is: I would like the user to be able to view (show), create (create) and destroy (destroy) their api keys from the show action (and view) of the development_center controller.

In other words:

development_center show method should render the show view, which - for that user - SHOULD be able to create and view API's keys that user creates.

I would also like to stop you from navigating in the browser to site.com/users/id/api_keys/id - instead it should redirect you to development_center show method for that user.

I am new to the concept of nested routes and have been reading the documentation on routing But I am unclear how to:

  1. redirect when you attempt to visit any user/id/api_keys/ route
  2. call api_keys controller actions in development_center views to allow you to create and show and destroy your own api keys.
È stato utile?

Soluzione

If the only problem is that you don't like api_keys in the URL, then you can change the path in your routes configuration instead:

resources :users do
  resources :api_keys, path: '/development_center'
end

This way you can continue using the ApiKeysController for these actions (as you logically should—since you are controlling access to API Keys using this controller) but the endpoints will look like /users/:user_id/development_center.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top