Commit f4019798

mo khan <mo@mokhan.ca>
2014-06-17 04:41:03
upload new photos using backbone.
1 parent e5b8e39
Changed files (8)
app
assets
javascripts
backbone
controllers
views
config
app/assets/javascripts/backbone/models/photo.js.coffee
@@ -11,4 +11,4 @@ class Cake.Collections.PhotosCollection extends Backbone.Collection
   url: '/api/v1/cakes/1/photos'
 
   initialize: (options) ->
-    console.log(options)
+    @url="/api/v1/cakes/#{options.cake_id}/photos"
app/assets/javascripts/backbone/routers/photos_router.js.coffee
@@ -1,15 +1,13 @@
 class Cake.Routers.PhotosRouter extends Backbone.Router
-  initialize: (options) ->
-    @photos = new Cake.Collections.PhotosCollection()
-
   routes:
-    ":cake_id/photos/new"      : "newPhoto"
+    "cakes/:cake_id/photos/new"      : "newPhoto"
     "photos/index"    : "index"
     "photos/:id/edit" : "edit"
     "photos/:id"      : "show"
     "photos/.*"        : "index"
 
   newPhoto: (cake_id) ->
+    @photos = new Cake.Collections.PhotosCollection(cake_id: cake_id)
     @view = new Cake.Views.Photos.NewView(collection: @photos)
     $("#backbone-content").html(@view.render().el)
 
app/assets/javascripts/backbone/templates/photos/new.jst.ejs
@@ -1,20 +1,18 @@
-<h1>New photo</h1>
-
-<form id="new-photo" name="photo">
-  <div class="field">
-    <label for="thumb_url"> thumb_url:</label>
-    <input type="text" name="thumb_url" id="thumb_url" value="<%= thumb_url %>" >
-  </div>
-
-  <div class="field">
-    <label for="large_url"> large_url:</label>
-    <input type="text" name="large_url" id="large_url" value="<%= large_url %>" >
-  </div>
-
-  <div class="actions">
-    <input type="submit" value="Create Photo" />
-  </div>
+<h1>Upload photo</h1>
 
+<form id="new-photo" name="photo" class="form-horizontal">
+  <fieldset>
+    <div class="control-group">
+      <label class="control-label" for="photo_image">Photo</label>
+      <div class="controls">
+        <input class="input-xxlarge" id="photo_image" name="image" type="file">
+      </div>
+    </div>
+    <div class="form-actions">
+      <button type="submit" class="btn btn-primary">Upload Photo</button>
+      <a href="#/" class="btn">Cancel</a>
+    </div>
+  </fieldset>
 </form>
 
 <a href="#/index">Back</a>
app/assets/javascripts/backbone/views/cakes/new_view.js.coffee
@@ -23,7 +23,7 @@ class Cake.Views.Cakes.NewView extends Backbone.View
     @collection.create(@model.toJSON(),
       success: (cake) =>
         @model = cake
-        window.location.hash = "/#{@model.id}/photos/new"
+        window.location.hash = "/cakes/#{@model.id}/photos/new"
 
       error: (cake, jqXHR) =>
         #@model.set({errors: $.parseJSON(jqXHR.responseText)})
app/controllers/api/v1/photos_controller.rb
@@ -2,6 +2,19 @@ module Api
   module V1
     class PhotosController < ApiController
       respond_to :json
+
+      def create
+        cake_id = params[:cake_id]
+        UploadPhoto.new.run(cake_id, photo_params)
+        @photo = Creation.find(cake_id).photos.last
+        respond_with(@photo)
+      end
+
+      private
+
+      def photo_params
+        params.require(:photo).permit(:image)
+      end
     end
   end
 end
app/views/api/v1/photos/_photo.json.jbuilder
@@ -0,0 +1,12 @@
+json.id photo.id
+json.id photo.creation_id
+json.content_type photo.content_type
+json.original_filename photo.original_filename
+json.latitude photo.latitude
+json.longitude photo.longitude
+json.sha256 photo.sha256
+json.thumb_url photo.url_for(:thumb)
+json.large_url photo.url_for(:large)
+json.original_url photo.url_for(:original)
+json.created_at photo.created_at
+json.updated_at photo.updated_at
app/views/api/v1/photos/create.json.jbuilder
@@ -0,0 +1,1 @@
+json.partial! 'photo', photo: @photo
config/routes.rb
@@ -47,7 +47,7 @@ Cake::Application.routes.draw do
   namespace :api, :defaults => { :format => 'json' }  do
     namespace :v1 do
       resources :cakes, :only => [:index, :show, :create, :update] do
-        resources :photos, :only => [:index]
+        resources :photos, :only => [:create]
       end
       resources :logins, :only => [:create]
     end