George Boot
George Boot

Reputation: 605

Ruby on Rails database driven router to support fully customizable urls

I'm planning to port our current cms (written in PHP) to Rails. All parts do well, except for one: routing.

Like most cms systems, the routing in our cms based on a database with pages, which are linked to modules, controllers and actions. On this approach a user can fully customize or specify it's own urls.

I know that Rails (and most (application) frameworks have the approach of defining routes in a file, but I hope this is possible.

The approach our users should have is:

  1. add new page
  2. select type (news, form, product, ...)
  3. select an item (select which form, blog or product should be displayed)
  4. enter a url for that page

Special the last point (4) is important. A user should be able to add form A to /contact-us, and form B to /clients/register-as-new-client e.g.

On a request the router needs to do a database query with the page url, to find out which controller, task and parameters should be dispatched.

Upvotes: 0

Views: 145

Answers (2)

Igbanam
Igbanam

Reputation: 6082

Although this solution isn't ReSTful, I think this should solve the issue.

Regardless, Rails uses routes in a file. You cannot change this since the framework heralds "convention over configiuration". All I can do is point you in a direction to minimize this.

There is a catchall route in Rails (on RailsCasts, and on StackOverflow) which you can use to direct all routing to one controller action. You may further customize the routing behaviour in that method

You could also make a route like…

:controller/:action => Controller::Action

…as is done in CodeIgniter, but now your methods have to have names like contact-us and register-as-a-new-client.

Upvotes: 0

firien
firien

Reputation: 1568

Question has been updated, and i don't think this is a valid answer anymore

we have a similar paging system. we use a routing glob. in routes.rb:

get 'pages/*lookup_path', to: 'pages#show', defaults: { format: 'html' }, as: 'page'

Just parse params[:lookup_path] in PagesController to suit your needs

'http://localhost/pages/users/'
  params[:lookup_path] #=> users/
'http://localhost/pages/users/23'
  params[:lookup_path] #=> users/23
'http://localhost/pages/people/1'
  params[:lookup_path] #=> people/1

Upvotes: 1

Related Questions