image uploading form working

This commit is contained in:
jywarren
2015-01-07 20:21:32 -05:00
parent 6ba4dd1bc1
commit 0945d55443
9 changed files with 20 additions and 17 deletions

View File

@@ -25,7 +25,7 @@ function addUploadedImageToSidebar($upload) {
jQuery(document).ready(function($) { jQuery(document).ready(function($) {
$('#fileupload').fileupload({ $('#fileupload').fileupload({
paramName: 'warpable[uploaded_data]', paramName: 'uploaded_data',
autoUpload: 'true', autoUpload: 'true',
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 10000000 maxFileSize: 10000000

View File

@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
#include OpenIdAuthentication # shouldn't be necessary!!
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception

View File

@@ -3,7 +3,7 @@ require 'open3'
class MapsController < ApplicationController class MapsController < ApplicationController
protect_from_forgery :except => [:export] protect_from_forgery :except => [:export]
before_filter :require_user, :only => [:create, :new, :edit, :update, :destroy] before_filter :require_login, :only => [:create, :new, :edit, :update, :destroy]
layout 'knitter2' layout 'knitter2'

View File

@@ -2,7 +2,6 @@ require 'uri'
# This controller handles the login/logout function of the site. # This controller handles the login/logout function of the site.
class SessionsController < ApplicationController class SessionsController < ApplicationController
include OpenIdAuthentication # shouldn't be necessary!!
#protect_from_forgery :except => [:create] #protect_from_forgery :except => [:create]
@@openid_url_base = "http://publiclab.org/people/" @@openid_url_base = "http://publiclab.org/people/"

View File

@@ -28,9 +28,9 @@ class WarpablesController < ApplicationController
end end
def create def create
@warpable = Warpable.new(params[:warpable]) @warpable = Warpable.new
@warpable.image = params[:uploaded_data]
@warpable.map_id = params[:map_id] @warpable.map_id = params[:map_id]
print params[:map_id]
map = Map.find(params[:map_id]) map = Map.find(params[:map_id])
map.updated_at = Time.now map.updated_at = Time.now
map.save map.save
@@ -40,7 +40,7 @@ class WarpablesController < ApplicationController
render :json => [@warpable.fup_json].to_json, render :json => [@warpable.fup_json].to_json,
:content_type => 'text/html' :content_type => 'text/html'
} }
format.json { render :json => {:files => [@warpable.fup_json]}, :status => :created, :location => @warpable.public_filename() } format.json { render :json => {:files => [@warpable.fup_json]}, :status => :created, :location => @warpable.image.url }
else else
format.html { render :action => "new" } format.html { render :action => "new" }
format.json { render :json => {:files => [@warpable.fup_error_json]}, :layout => false} format.json { render :json => {:files => [@warpable.fup_error_json]}, :layout => false}

View File

@@ -1,5 +1,6 @@
require 'open3' require 'open3'
class Map < ActiveRecord::Base class Map < ActiveRecord::Base
attr_accessible :author, :name, :lat, :lon, :location, :description
before_validation :update_name before_validation :update_name
validates_presence_of :name,:author,:lat,:lon validates_presence_of :name,:author,:lat,:lon
validates_uniqueness_of :name validates_uniqueness_of :name
@@ -30,6 +31,7 @@ class Map < ActiveRecord::Base
end end
end end
# impose URL-safe sanity on name
def validate def validate
self.name != 'untitled' self.name != 'untitled'
self.name = self.name.gsub(' ','-').gsub('_','-').downcase self.name = self.name.gsub(' ','-').gsub('_','-').downcase
@@ -41,6 +43,7 @@ class Map < ActiveRecord::Base
self.password = Password::update(self.password) if self.password != "" self.password = Password::update(self.password) if self.password != ""
end end
# this might have been for a migration; grep for it
def update_name def update_name
self.name = self.name.gsub(/\W/, '-').downcase self.name = self.name.gsub(/\W/, '-').downcase
end end

View File

@@ -21,18 +21,18 @@ class Warpable < ActiveRecord::Base
#Json formatting for file upload plugin #Json formatting for file upload plugin
def fup_json def fup_json
{"name" => read_attribute(:filename), {"name" => read_attribute(:image_filename),
"size" => read_attribute(:size), "size" => read_attribute(:image_size),
"url" => self.public_filename(:medium), "url" => self.image.url(:medium),
"id" => self.read_attribute(:id), "id" => self.read_attribute(:id),
"thumbnail_url" => self.public_filename(:thumb), "thumbnail_url" => self.image.url(:thumb),
"delete_url" => public_filename(self), "delete_url" => self.image.url,
"delete_type" => "DELETE"} "delete_type" => "DELETE"}
end end
def fup_error_json def fup_error_json
{"name" => read_attribute(:filename), {"name" => read_attribute(:image_filename),
"size" => read_attribute(:size), "size" => read_attribute(:image_size),
"error" => self.errors["base"]} "error" => self.errors["base"]}
end end

View File

@@ -24,11 +24,11 @@
<% @maps.each do |map| %> <% @maps.each do |map| %>
<div class="map col-md-3 <%= 'odd' if odd %>"> <div class="map col-md-3 <%= 'odd' if odd %>">
<% if !map.private && map.warpables.length > 0 %> <% if !map.private && map.warpables.length > 0 %>
<a href="/maps/<%= map.id %>"> <a href="/map/<%= map.id %>">
<img src="<%= map.warpables.first.image.url(:small) %>" /> <img src="<%= map.warpables.first.image.url(:small) %>" />
</a> </a>
<% end %> <% end %>
<h3><%= link_to map.name.capitalize, map %></a></h3> <h3><a href="/map/<%= map.id %>"><%= map.name.capitalize %></a></h3>
<p> <p>
<small>by <a href="/profile/<%= map.author %>"><%= map.author %></a> | <%= map.location %></small></p> <small>by <a href="/profile/<%= map.author %>"><%= map.author %></a> | <%= map.location %></small></p>
<p class="description"><small> <p class="description"><small>

View File

@@ -10,7 +10,7 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div> <div>
<% form_for [@map, Warpable.new], :html => { :multipart => true, :id => "fileupload" } do |f| %> <%= form_for [@map, Warpable.new], :html => { :multipart => true, :id => "fileupload" } do |f| %>
<input id="warpable_map_id" name="map_id" type="hidden" value="<%= @map_id %>" /> <input id="warpable_map_id" name="map_id" type="hidden" value="<%= @map_id %>" />
<div class="row"> <div class="row">
@@ -104,4 +104,4 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>