# HG changeset patch # User Nattee Niparnan # Date 2014-09-22 15:46:06 # Node ID 9bd14bb6e95dbb38de71654095b60a5facb1faad # Parent 04ddd5707db69399d685d2d6c60814b46093b148 add option to update PDF file of a problem in /problem/edit/:id (grafted from 15b2554bf365f8639b5e94db4f1d9a277007808a) diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -89,8 +89,30 @@ render :action => 'edit' and return end end + if params[:file] and params[:file].content_type != 'application/pdf' + flash[:notice] = 'Error: Uploaded file is not PDF' + render :action => 'edit' and return + end if @problem.update_attributes(params[:problem]) flash[:notice] = 'Problem was successfully updated.' + unless params[:file] == nil or params[:file] == '' + flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' + out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" + if not FileTest.exists? out_dirname + Dir.mkdir out_dirname + end + + out_filename = "#{out_dirname}/#{@problem.name}.pdf" + if FileTest.exists? out_filename + File.delete out_filename + end + + File.open(out_filename,"wb") do |file| + file.write(params[:file].read) + end + @problem.description_filename = "#{@problem.name}.pdf" + @problem.save + end redirect_to :action => 'show', :id => @problem else render :action => 'edit' diff --git a/app/views/problems/_form.html.erb b/app/views/problems/_form.html.erb --- a/app/views/problems/_form.html.erb +++ b/app/views/problems/_form.html.erb @@ -48,5 +48,7 @@


<%= text_field 'problem', 'url' %>

+

Task PDF <%= file_field_tag 'file' %>

+ diff --git a/app/views/problems/edit.html.erb b/app/views/problems/edit.html.erb --- a/app/views/problems/edit.html.erb +++ b/app/views/problems/edit.html.erb @@ -1,6 +1,6 @@

Editing problem

-<%= form_tag :action => 'update', :id => @problem do %> +<%= form_tag({action: 'update', id: @problem},multipart: true) do %> <%= render :partial => 'form' %> <%= submit_tag 'Edit' %> <% end %>