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
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"
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})
--- /dev/null
+<% form_for :trace, @trace, :url => { :action => "create" }, :html => { :multipart => true } do |f| %>
+<table>
+ <tr><td align="right">Upload GPX File</td><td><%= f.file_field :gpx_file, :size => 50, :maxlength => 255 %></td></tr>
+ <tr><td align="right">Description</td><td><%= f.text_field :description, :size => 50, :maxlength => 255 %></td></tr>
+ <tr><td align="right">Tags</td><td><%= f.text_field :tagstring, :size => 50, :maxlength => 255 %></td></tr>
+ <tr><td align="right">Public?</td><td><%= f.check_box :public %></td></tr>
+ <tr><td></td><td><%= submit_tag 'Upload' %> | <a href="http://wiki.openstreetmap.org/index.php/Upload">Help</a></td></tr>
+</table>
+<% end %>
<%= error_messages_for 'trace' %>
-<% form_tag({:action => 'create'}, :multipart => true) do %>
-<table>
-<tr><td align="right">Upload GPX File</td><td><%= file_field('trace', 'gpx_file', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">Description</td><td><%= text_field('trace', 'description', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">Tags</td><td><%= text_field('trace', 'tagstring', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">Public?</td><td><%= check_box('trace', 'public') %></td></tr>
-<tr><td></td><td>
-<%= submit_tag 'Upload' %> | <a href="http://wiki.openstreetmap.org/index.php/Upload">help</a>
-</td></tr>
-</table>
-<% end %>
+<%= render :partial => 'trace_form' %>
<%= render :partial => 'trace_header' %>
-
-<% form_tag({:action => 'create'}, :multipart => true) do %>
-<table>
-<table>
-<tr><td align="right">upload GPX file:</td><td><%= file_field('trace', 'gpx_file', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">description:</td><td><%= text_field('trace', 'description', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">tags:</td><td><%= text_field('trace', 'tagstring', {:size => 50, :maxlength => 255}) %></td></tr>
-<tr><td align="right">public?</td><td><%= check_box('trace', 'public', {:checked => 'checked'}) %></td></tr>
-<tr><td></td><td>
-<%= submit_tag 'Upload' %> | <a href="http://wiki.openstreetmap.org/index.php/Upload">help</a>
-</td></tr>
-</table>
-<% end %>
-
+<%= render :partial => 'trace_form' %>
<%= render :partial => 'trace_list' %>