From: Tom Hughes Date: Tue, 25 Sep 2007 23:18:32 +0000 (+0000) Subject: Improve consistency of trace upload forms and error handling. X-Git-Tag: live~8102 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/3db4ac9a8e8dd94bc288e98ff353452e401d0a59?hp=68637a381c5e08e26ada45718f782486db831227 Improve consistency of trace upload forms and error handling. Fixes #543 and #544. --- 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' %>