Controllers

Controllers map the application to a specified base route. Each controller also contains routes that extend from the base route.

CLI tasks

Example

app/controllers/product_controller.rb
class ProductController < Eucalypt::Controller(route: '/products')
  helpers ProductHelper if defined? ProductHelper
  
  # Route: /products/
  get '/' do
    @products = Product.all
    erb :'products/browse'
  end
  
  # Route: /products/:id
  get '/:id' do |id|
    @product = Product.find id
    erb :'products/show'
  end
end
  • The base route for the controller is specified in line 1:

    ... < Eucalypt::Controller(route: '/products')
  • Routes defined within this controller are relative to the base route (/products). This means that:

    • get '/' maps to the route /products/

    • get '/:id' maps to the route /products/:id

Main controller

The main controller (MainController) works slightly differently. Since it inherits directly from ApplicationController, you cannot specify a base route, as the controller is always mounted at /.

Last updated