Commit d557bfe

mo khan <mo@mokhan.ca>
2013-06-08 13:30:47
add category to cake
1 parent cada686
app/controllers/cakes_controller.rb
@@ -9,10 +9,12 @@ class CakesController < ApplicationController
 
   def new
     @cake = Cake.new
+    @categories = Category.all
   end
 
   def edit
     @cake = Cake.find(params[:id])
+    @categories = Category.all
   end
 
   def create
app/models/cake.rb
@@ -1,5 +1,6 @@
 class Cake < ActiveRecord::Base
-  attr_accessible :name, :photo, :photo_cache
+  attr_accessible :name, :photo, :photo_cache, :category_id
+  belongs_to :category
   mount_uploader :photo, PhotoUploader
 
   def slug
app/models/category.rb
@@ -1,3 +1,4 @@
 class Category < ActiveRecord::Base
   attr_accessible :name, :slug
+  has_many :cakes
 end
app/views/cakes/_form.html.erb
@@ -13,6 +13,12 @@
         <%= f.hidden_field :photo_cache %>
       </div>
     </div>
+    <div class="control-group">
+      <%= f.label :category_id, :class => "control-label" %>
+      <div class="controls">
+        <%= f.select :category_id, options_from_collection_for_select(@categories, :id, :name) %>
+      </div>
+    </div>
   </fieldset>
   <div class="form-actions">
     <button type="submit" class="btn btn-primary">Save</button>
db/migrate/20130608132346_add_category_id_to_cake.rb
@@ -0,0 +1,5 @@
+class AddCategoryIdToCake < ActiveRecord::Migration
+  def change
+    add_column :cakes, :category_id, :integer
+  end
+end
db/schema.rb
@@ -11,13 +11,14 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130608131324) do
+ActiveRecord::Schema.define(:version => 20130608132346) do
 
   create_table "cakes", :force => true do |t|
     t.string   "name"
-    t.datetime "created_at", :null => false
-    t.datetime "updated_at", :null => false
+    t.datetime "created_at",  :null => false
+    t.datetime "updated_at",  :null => false
     t.string   "photo"
+    t.integer  "category_id"
   end
 
   create_table "categories", :force => true do |t|