OSDN Git Service

Do validations on Task and Apk
authoreagletmt <eagletmt@gmail.com>
Fri, 19 Oct 2012 18:23:13 +0000 (03:23 +0900)
committereagletmt <eagletmt@gmail.com>
Fri, 19 Oct 2012 18:23:13 +0000 (03:23 +0900)
CutenServer/app/controllers/tasks_controller.rb
CutenServer/app/models/apk.rb
CutenServer/app/models/task.rb

index 150f809..964290c 100644 (file)
@@ -33,26 +33,22 @@ class TasksController < ApplicationController
     @task.lecture = Lecture.find params[:lecture_id]
     @apk = Apk.new params[:apk]
     uploaded_file = params[:apkfile] && params[:apkfile][:file]
+    if uploaded_file.nil?
+      @noapk = I18n.t 'task.create.noapk'
+    end
+    @apk.task = @task
 
     respond_to do |format|
       format.html do
         if teacher and @task.lecture.teacher.id == teacher.id
-          if uploaded_file
-            # TODO: check if the uploaded apk is correct
-            if @apk.save
-              @apk.store_file uploaded_file.tempfile
-
-              @task.apks << @apk
-              if @task.save
-                redirect_to @task.lecture, :notice => I18n.t('task.create.success')
-              else
-                render :action => 'new'
-              end
-            else
-              render :action => 'new'
-            end
+          # TODO: check if the uploaded apk is correct
+          if !@noapk and @task.save and @apk.save
+            @apk.store_file uploaded_file.tempfile
+            redirect_to @task.lecture, :notice => I18n.t('task.create.success')
           else
-            @noapk = I18n.t('task.create.noapk')
+            if @task.persisted?
+              @task.destroy
+            end
             render :action => 'new'
           end
         else
index 119c606..3af856d 100644 (file)
@@ -2,6 +2,8 @@ class Apk < ActiveRecord::Base
   belongs_to :task
   attr_accessible :name, :revision
 
+  validates_presence_of :task, :name, :revision
+
   include JsonWithoutTimestamps
   def as_json(options = {})
     super options.merge({:except => [:task_id]})
index 700cb7a..a6655b4 100644 (file)
@@ -3,6 +3,8 @@ class Task < ActiveRecord::Base
   has_many :apks, :order => 'revision DESC'
   attr_accessible :name
 
+  validates_presence_of :lecture, :name
+
   include JsonWithoutTimestamps
   def as_json(options = {})
     super options.merge({:methods => [:apk], :except => [:lecture_id]})