From 09a2882dbf4d1d4d3309946929cc0252d73e2369 Mon Sep 17 00:00:00 2001 From: eagletmt Date: Sat, 20 Oct 2012 03:23:13 +0900 Subject: [PATCH] Do validations on Task and Apk --- CutenServer/app/controllers/tasks_controller.rb | 26 +++++++++++-------------- CutenServer/app/models/apk.rb | 2 ++ CutenServer/app/models/task.rb | 2 ++ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CutenServer/app/controllers/tasks_controller.rb b/CutenServer/app/controllers/tasks_controller.rb index 150f809..964290c 100644 --- a/CutenServer/app/controllers/tasks_controller.rb +++ b/CutenServer/app/controllers/tasks_controller.rb @@ -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 diff --git a/CutenServer/app/models/apk.rb b/CutenServer/app/models/apk.rb index 119c606..3af856d 100644 --- a/CutenServer/app/models/apk.rb +++ b/CutenServer/app/models/apk.rb @@ -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]}) diff --git a/CutenServer/app/models/task.rb b/CutenServer/app/models/task.rb index 700cb7a..a6655b4 100644 --- a/CutenServer/app/models/task.rb +++ b/CutenServer/app/models/task.rb @@ -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]}) -- 2.11.0