How to make pages with a "template" controller and view in VoltRb

66 views Asked by At

I'm having a problem in Volt where I need a bunch of pages with a "template" controller and view. Let me explain this in a little more detail:

Say, for example, I have a site about books. I have a page at http://localhost:3000/books. It serves as an index to other pages, e.g. http://localhost:3000/books/fantasy/lord_of_the_rings. I want all of the pages featuring books to have a single controller and view, so that I don't have to manually add in a controller and view for each book that I want to add. Rather, I just want to add in content to each page featuring a book to keep my workflow as DRY as possible.

So, the file structure under app/main/views would look like this:

views
├── books
│   ├── index.html
│   └── template.html
└── main
    ├── index.html
    └── main.html

And the file structure under app/main/controllers would look like this:

controllers
├── books_controller.rb
└── main_controller.rb

The problem is that I don't know how to set this up in app/main/config/routes.rb since I guess that's where pages' URLs are defined and their controllers, actions, etc. How do I go about achieving something like this in Volt?

1

There are 1 answers

1
Julian Fahrer On BEST ANSWER

You could for example use the following routes:

client '/books', controller: 'books', action: 'index'
client '/books/{{ genre }}/{{ title }}', controller: 'books', action: 'template'

This will route both requests to the books controller. And it will make the genre and title available via the params collection in the template controller action(s) and the view. You can then use this information to load the correct book from the store (or wherever).

You can read more about routes here: http://docs.voltframework.com/en/docs/routes_file.html