CubaTutorial

From Cubawiki

Jump to: navigation, search

ortacodeln Let's begin with designing an user interface as ERB template. We want at least a list of cars, so we define a view:

  <!-- in /my/webroot/my_app/views/car_list.rhtml -->
  
  <div class="car_list">
  <%= link_to Car.list(:from => from.to_i+10, :to => to.to_i+10), 'next' %>

  <% cars.each do |car| %>
    
    <div class="car_list_entry">
      <div class="toolbar">
         <%= link_to Car.update(car), 'edit' %> 
         <%= link_to Car.delete(car), 'delete' %>  
      </div>
      <%= car.name %>
    </div>
    
  <% end # cars.each %>
  </div>

Learn more about views

A Controller using this template looks like this:

 # in /my/webroot/my_app/controllers/car.rb:
 
 require('cuba/controller')
 
 class Car_Controller < Cuba::Controller
 
   # URL:  /my_app/Car/list/from-0--to-10.html
   def list 
     set_default_param(:from, 0)  # If there are no parameters given for 
     set_default_param(:to, 20)   # 'from' and 'to', use default values
 
     cars = Car.find( param(:from), param(:to) ).entities
     render_view(:car_list, :cars => cars)
   end
 
 end # class

Learn more about Cuba::Controller

Personal tools