OSDN Git Service

Add /lectures page
[sharp4k/CUTEn.git] / CutenServer / app / controllers / lectures_controller.rb
index 553bc97..210b25b 100644 (file)
@@ -4,43 +4,50 @@ class LecturesController < ApplicationController
   def index
     if params[:ids] and params[:ids].is_a? Array
       @lectures = Lecture.where :id => params[:ids]
-      render json: @lectures.as_json(:methods => [:tasks])
+      respond_to do |format|
+        format.json { render json: @lectures.as_json(:methods => [:tasks]) }
+      end
     else
       @lectures = Lecture.all
-      render json: @lectures
+      respond_to do |format|
+        format.html
+        format.json { render json: @lectures }
+      end
     end
   end
 
   # GET /lectures/new
   def new
-    authenticate_user!
-    @lecture = Lecture.new
-    @lecture.teacher = current_user.teacher
+    if user_signed_in?
+      respond_to do |format|
+        format.html { redirect_to root_path, :alert => I18n.t('teacher.not') }
+      end
+    else
+      authenticate_teacher!
+      @lecture = Lecture.new
+      @lecture.teacher = current_teacher
 
-    respond_to do |format|
-      if @lecture.teacher
+      respond_to do |format|
         format.html # new.html.haml
-      else
-        format.html { redirect_to root_path, :alert => I18n.t('teacher.not') }
       end
     end
   end
 
   # POST /lectures
   def create
-    authenticate_user!
-    @lecture = Lecture.new params[:lecture]
-    @lecture.teacher = current_user.teacher
-
     respond_to do |format|
-      if @lecture.teacher
+      if user_signed_in?
+        format.html { redirect_to root_path, :alert => I18n.t('teacher.not') }
+      else
+        authenticate_teacher!
+        @lecture = Lecture.new params[:lecture]
+        @lecture.teacher = current_teacher
+
         if @lecture.save
-          format.html { redirect_to @lecture, :notice => I18n.t('lectures.created') }
+          format.html { redirect_to @lecture, :notice => I18n.t('lecture.created') }
         else
           format.html { render :action => 'new' }
         end
-      else
-        format.html { redirect_to root_path, :alert => I18n.t('teacher.not') }
       end
     end
   end