From 3db4ac9a8e8dd94bc288e98ff353452e401d0a59 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 25 Sep 2007 23:18:32 +0000 Subject: [PATCH] Improve consistency of trace upload forms and error handling. Fixes #543 and #544. --- app/controllers/trace_controller.rb | 38 +++++++++++++++++------------ app/views/trace/_trace_form.rhtml | 9 +++++++ app/views/trace/create.rhtml | 12 +-------- app/views/trace/mine.rhtml | 15 +----------- 4 files changed, 33 insertions(+), 41 deletions(-) create mode 100644 app/views/trace/_trace_form.rhtml diff --git a/app/controllers/trace_controller.rb b/app/controllers/trace_controller.rb index 3eb7f5c7e..7c5bd7db0 100644 --- a/app/controllers/trace_controller.rb +++ b/app/controllers/trace_controller.rb @@ -94,17 +94,26 @@ class TraceController < ApplicationController end def create - name = params[:trace][:gpx_file].original_filename.gsub(/[^a-zA-Z0-9.]/, '_') # This makes sure filenames are sane + logger.info(params[:trace][:gpx_file].class.name) + if params[:trace][:gpx_file].respond_to?(:read) + do_create(params[:trace][:gpx_file], params[:trace][:tagstring], + params[:trace][:description], params[:trace][:public]) - do_create(name, params[:trace][:tagstring], params[:trace][:description], params[:trace][:public]) do |f| - f.write(params[:trace][:gpx_file].read) - end - - if @trace.id - logger.info("id is #{@trace.id}") - flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion." + if @trace.id + logger.info("id is #{@trace.id}") + flash[:notice] = "Your GPX file has been uploaded and is awaiting insertion in to the database. This will usually happen within half an hour, and an email will be sent to you on completion." - redirect_to :action => 'mine' + redirect_to :action => 'mine' + end + else + @trace = Trace.new({:name => "Dummy", + :tagstring => params[:trace][:tagstring], + :description => params[:trace][:description], + :public => params[:trace][:public], + :inserted => false, :user => @user, + :timestamp => Time.now}) + @trace.valid? + @trace.errors.add(:gpx_file, "can't be blank") end end @@ -259,11 +268,7 @@ class TraceController < ApplicationController def api_create if request.post? - name = params[:file].original_filename.gsub(/[^a-zA-Z0-9.]/, '_') # This makes sure filenames are sane - - do_create(name, params[:tags], params[:description], params[:public]) do |f| - f.write(params[:file].read) - end + do_create(params[:file], params[:tags], params[:description], params[:public]) if @trace.id render :text => @trace.id.to_s, :content_type => "text/plain" @@ -279,10 +284,11 @@ class TraceController < ApplicationController private - def do_create(name, tags, description, public) + def do_create(file, tags, description, public) + name = file.original_filename.gsub(/[^a-zA-Z0-9.]/, '_') filename = "/tmp/#{rand}" - File.open(filename, "w") { |f| yield f } + File.open(filename, "w") { |f| f.write(file.read) } @trace = Trace.new({:name => name, :tagstring => tags, :description => description, :public => public}) diff --git a/app/views/trace/_trace_form.rhtml b/app/views/trace/_trace_form.rhtml new file mode 100644 index 000000000..299845015 --- /dev/null +++ b/app/views/trace/_trace_form.rhtml @@ -0,0 +1,9 @@ +<% form_for :trace, @trace, :url => { :action => "create" }, :html => { :multipart => true } do |f| %> + + + + + + +
Upload GPX File<%= f.file_field :gpx_file, :size => 50, :maxlength => 255 %>
Description<%= f.text_field :description, :size => 50, :maxlength => 255 %>
Tags<%= f.text_field :tagstring, :size => 50, :maxlength => 255 %>
Public?<%= f.check_box :public %>
<%= submit_tag 'Upload' %> | Help
+<% end %> diff --git a/app/views/trace/create.rhtml b/app/views/trace/create.rhtml index db9dc385c..94f403951 100644 --- a/app/views/trace/create.rhtml +++ b/app/views/trace/create.rhtml @@ -2,14 +2,4 @@ <%= error_messages_for 'trace' %> -<% form_tag({:action => 'create'}, :multipart => true) do %> - - - - - - -
Upload GPX File<%= file_field('trace', 'gpx_file', {:size => 50, :maxlength => 255}) %>
Description<%= text_field('trace', 'description', {:size => 50, :maxlength => 255}) %>
Tags<%= text_field('trace', 'tagstring', {:size => 50, :maxlength => 255}) %>
Public?<%= check_box('trace', 'public') %>
-<%= submit_tag 'Upload' %> | help -
-<% end %> +<%= render :partial => 'trace_form' %> diff --git a/app/views/trace/mine.rhtml b/app/views/trace/mine.rhtml index 291217753..7deaa1831 100644 --- a/app/views/trace/mine.rhtml +++ b/app/views/trace/mine.rhtml @@ -1,16 +1,3 @@ <%= render :partial => 'trace_header' %> - -<% form_tag({:action => 'create'}, :multipart => true) do %> - -
- - - - - -
upload GPX file:<%= file_field('trace', 'gpx_file', {:size => 50, :maxlength => 255}) %>
description:<%= text_field('trace', 'description', {:size => 50, :maxlength => 255}) %>
tags:<%= text_field('trace', 'tagstring', {:size => 50, :maxlength => 255}) %>
public?<%= check_box('trace', 'public', {:checked => 'checked'}) %>
-<%= submit_tag 'Upload' %> | help -
-<% end %> - +<%= render :partial => 'trace_form' %> <%= render :partial => 'trace_list' %> -- 2.39.5