Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

aluink

By: a guest on Oct 22nd, 2009  |  syntax: Rails  |  size: 2.43 KB  |  views: 75  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. ======
  3. Schema
  4. ======
  5.  
  6.  create_table "logs", :force => true do |t|
  7.     t.date     "date"
  8.     t.text     "description"
  9.     t.string   "title"
  10.     t.string   "summary"
  11.     t.integer  "car_id"
  12.     t.datetime "created_at"
  13.     t.datetime "updated_at"
  14.     t.integer  "maintenance_id"
  15.     t.integer  "mileage"
  16.     t.string   "rtype"
  17.     t.string   "oil_brand"
  18.     t.string   "oil_type"
  19.     t.string   "oil_filter_type"
  20.   end
  21.  
  22. ======
  23. Models
  24. ======
  25.  
  26. class Log < ActiveRecord::Base
  27.         belongs_to :car
  28.         has_and_belongs_to_many :tags
  29.   set_inheritance_column :rtype
  30.  
  31.   def self.types
  32.     v = Array.new()
  33.    
  34.     v.push(nil)
  35.     v.push("OilChange")
  36.     v.push("BrakePads")
  37.     v.push("TimingBelt")
  38.    
  39.     return v
  40.   end
  41.  
  42. end
  43.  
  44. class Car < ActiveRecord::Base
  45.         has_many :logs
  46. end
  47.  
  48. class OilChange < Log
  49. end
  50.  
  51. ===========
  52. Controllers
  53. ===========
  54.  
  55.   # POST /logs
  56.   # POST /logs.xml
  57.   def create
  58.     @log = Log.new(params[:log])
  59.    
  60.     @log.rtype = params[:log][:rtype]
  61.    
  62.     respond_to do |format|
  63.       if @log.save
  64.         flash[:notice] = 'Log was successfully created.'
  65.         format.html { redirect_to(@log) }
  66.         format.xml  { render :xml => @log, :status => :created, :location => @log }
  67.       else
  68.         format.html { render :action => "new" }
  69.         format.xml  { render :xml => @log.errors, :status => :unprocessable_entity }
  70.       end
  71.     end
  72.   end
  73.  
  74.   # GET /logs/new
  75.   # GET /logs/new.xml
  76.   def new
  77.     @log = Log.new
  78.  
  79.     @m_types = Log.types
  80.     @cars = Car.all
  81.    
  82.     print @maintenance
  83.  
  84.                 if not params[:car].nil?
  85.                         @log.car = params[:car]
  86.                 end
  87.  
  88.     respond_to do |format|
  89.       format.html # new.html.erb
  90.       format.xml  { render :xml => @log }
  91.     end
  92.   end
  93.  
  94. ====
  95. View
  96. ====
  97.  
  98. <h1>New log</h1>
  99.  
  100. <% form_for(@log) do |f| %>
  101.   <%= f.error_messages %>
  102.  
  103.   <p>
  104.     <%= f.label :date %><br />
  105.     <%= f.date_select :date %>
  106.   </p>
  107.   <p>
  108.     <%= f.label :title %><br />
  109.     <%= f.text_field :title %>
  110.   </p>
  111.   <p>
  112.     <%= f.label :summary %><br />
  113.     <%= f.text_field :summary %>
  114.   </p>
  115.   <p>
  116.     <%= f.label :car %><br />
  117.                 <%= collection_select(:log, :car_id, @cars, :id, :name, {}, {}) %>
  118.   </p>
  119.   <p>
  120.         <%= f.label :rtype %>
  121.         <%= f.select :rtype, @m_types  %>
  122.   </p>
  123.   <p>
  124.     <%= f.label :description %><br />
  125.     <%= f.text_area :description %>
  126.   </p>
  127.   <p>
  128.     <%= f.submit 'Create' %>
  129.   </p>
  130. <% end %>
  131.  
  132. <%= link_to 'Back', logs_path %>