Cássio
Cássio

Reputation: 649

Rails root permalink routing

In Rails, the standard routing to objects is nested to a Model's name, example.com/model/object_id.

Is it anyhow possible to access objects without the Model part, so example.com/object_id shadowly accesses example.com/model/object_id?

Upvotes: 1

Views: 60

Answers (2)

akbarbin
akbarbin

Reputation: 5105

Rails includes routes like you said. You can add constraints to determine object_id is integer or string.

get '/:id', to: 'articles#show', constraints: { id: /^\d/ }

This is for more information about routes constraint.

Upvotes: 2

user2536065
user2536065

Reputation:

What you are first describing are the RESTful routes provided by the resources template in the rails router.

You can define different routes in the config/routes.rb file.

And for resources, you can provide a path option, where you can define a path.

resources :models, path: "/" 

Will provide models resources at the route path. So a GET request to "/" would fire the "models#index" action and "/1/edit" would delegate to "models#edit"

Upvotes: 1

Related Questions