Igor Martins
Igor Martins

Reputation: 2047

How to hide 'id' in url and show nothing?

To access a dashboard this is the url: http://localhost:3000/dashboards/1 I want to hide idand show nothing in the url to do this: http://localhost:3000/dashboards/ I see another questions but all of it show to substitute id for name for example. How can I just hide the id?

Upvotes: 1

Views: 1969

Answers (2)

Simone Carletti
Simone Carletti

Reputation: 176402

Map the controller as a resource instead of resources. In your routes.rb file change

resources :dashboards

to

resource :dashboard

This assumes your controller is called DashboardsController. If not, pass the appropriate controller name.

resource :dashboard, controller: 'whatever_controller'

You may also want to restrict to the show action only, unless you have other actions.

resource :dashboard, only: [:show]

This will create the route

GET /dashboard

If you don't limit the actions, you'll get also

GET /dashboard/new
POST /dashboard
PATCH /dashboard
DELETE /dashboard

Upvotes: 1

Justin D.
Justin D.

Reputation: 101

You just need to define a new route in routes.rb. If you're using resources :dashboards then that path is already defined and conventionally reserved as an index to list all available objects. Something like this should work:

get 'dashboards' => 'dashboards#show'

Note also that if you need the id in the controller you can no longer rely on params to grab the id as it's no longer in the url and available. You will have to use sessions instead.

Upvotes: 1

Related Questions