Commit 78b5584

mo khan <mo@mokhan.ca>
2013-05-26 04:15:40
mount photo uploader on to cake
1 parent 0c9282a
Changed files (6)
app/models/cake.rb
@@ -1,3 +1,4 @@
 class Cake < ActiveRecord::Base
-  attr_accessible :name
+  attr_accessible :name, :photo, :photo_cache
+  mount_uploader :photo, PhotoUploader
 end
app/views/cakes/_form.html.erb
@@ -1,4 +1,4 @@
-<%= form_for(@cake) do |f| %>
+<%= form_for(@cake, html: { multipart: true }) do |f| %>
   <% if @cake.errors.any? %>
     <div id="error_explanation">
       <h2><%= pluralize(@cake.errors.count, "error") %> prohibited this cake from being saved:</h2>
@@ -15,6 +15,11 @@
     <%= f.label :name %><br />
     <%= f.text_field :name %>
   </div>
+  <div class="field">
+    <%= f.label :photo %><br />
+    <%= f.file_field :photo %>
+    <%= f.hidden_field :photo_cache %>
+  </div>
   <div class="actions">
     <%= f.submit %>
   </div>
app/views/cakes/index.html.erb
@@ -1,22 +1,22 @@
+<div class="row">
+  <div class="span1 pull-right">
+    <%= link_to "new", new_cake_path, class: 'btn' %>
+  </div>
+</div>
 <div class="row">
   <div class="span12">
-    <div id="cakes-div" class="row">
-      <div class="span12">
-        <%= link_to "new", new_cake_path %>
-      <ul class="thumbnails">
-        <% @cakes.each do |cake| %>
-          <div class="span4">
-            <div class="thumbnail">
-              <a href="<%= url_for cake %>"><img src="" /></a>
-              <div class="caption">
-                <h3><a href="<%= url_for cake %>"><%= cake.name %></a></h3>
-                <p><a class="btn" href="<%= url_for cake %>">View</a></p>
-              </div>
-            </div>
+    <ul class="thumbnails">
+      <% @cakes.each do |cake| %>
+        <div class="span4">
+        <div class="thumbnail">
+          <a href="<%= url_for cake %>"><img src="<%= cake.photo.url %>" /></a>
+          <div class="caption">
+            <h3><a href="<%= url_for cake %>"><%= cake.name %></a></h3>
+            <p><a class="btn" href="<%= url_for cake %>">View</a></p>
           </div>
-        <% end %>
-      </ul>
+        </div>
       </div>
-    </div>
+    <% end %>
+    </ul>
   </div>
 </div>
db/migrate/20130526040312_add_photo_to_cakes.rb
@@ -0,0 +1,5 @@
+class AddPhotoToCakes < ActiveRecord::Migration
+  def change
+    add_column :cakes, :photo, :string
+  end
+end
db/schema.rb
@@ -11,12 +11,13 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130526024824) do
+ActiveRecord::Schema.define(:version => 20130526040312) do
 
   create_table "cakes", :force => true do |t|
     t.string   "name"
     t.datetime "created_at", :null => false
     t.datetime "updated_at", :null => false
+    t.string   "photo"
   end
 
 end
public/uploads/cake/photo/1/Couch_Cake__3.jpg
Binary file