Reform gem: relations between tables in form

212 Views Asked by At

I'm trying to build a service form for my web app, using Rails 4.0, slim, simple_form gem, and reform gem.

The services table 'belongs to' city, vehicle_type, driver and customer tables.

The service model:

class Service < ActiveRecord::Base
  has_paper_trail

  # Constants
  ACCEPTED_BRANCH_OFFICES = [ 'diadema', 'valinhos' ]
  ACCEPTED_SERVICE_TYPES = [ 'retail', 'contract' ]
  ACCEPTED_PRICING_TYPES = [ 'fixed', 'not_fixed' ]
  ACCEPTED_MANAGEMENT_STATUSES = [ "waiting_confirmation", 'confirmed', 'canceled']      
  ACCEPTED_PAYMENT_METHODS = [ 'contract', 'billet_banking' ]

  # relations
  belongs_to :customer
  belongs_to :one_way_travel_driver, class_name: 'Driver'
  belongs_to :return_trip_driver, class_name: 'Driver'
  belongs_to :origin_city, class_name: 'City'
  belongs_to :destiny_city, class_name: 'City'
  belongs_to :vehicle_type
  has_one :configuration_table

  # validations
  validates :origin_city, presence: true
  validates :destiny_city, presence: true
  validates :customer_id, presence: true
  validates :one_way_travel_driver, presence: true
  validates :return_trip_driver, presence: true
  validates :vehicle_type_id, presence: true  
  [...]
end

And part of the service form:

= f.input :origin_city_id, collection: City.order(:name)
= f.input :destiny_city_id, collection: City.order(:name)
= f.input :one_way_travel_driver_id, collection: Driver.order(:name)
= f.input :return_trip_driver_id, collection: Driver.order(:name)

I want to get origin_city_id, destiny_city_id, one_way_travel_driver_id and return_trip_driver_id (from City and Driver tables) for the form.

How can I do that using Reform gem and Form Object? Do I need to use composition?

0

There are 0 best solutions below