实话说不是一个好习惯,不过我还是没有适应测试驱动开发的思路,不过说回来进度比较紧张,暂且还来不太急补测试。
今天开始做后台中的课程管理部分,幸运的是,课程的index界面队友已经写好了,不幸的是,比较乱,也少东西,也不太适合后台管理的逻辑,所以要好好改一改。保留了show、destroy、edit这种用得上的功能,删除了显示下属知识的部分(话说显示个对象id也完全没有用不是),增加了展示所属学院和教授教师的按钮。
做了所属学院部分,逻辑和显示都和学院下属课程很类似,于是大部分照搬,大概是这样的:
里面有两个方法新建和删除。删除关联直接指向了学院那边写好的方法,不过这里有个问题就是那边的redirect_to会指向那边的学院,在考虑要不要直接复制过来换一个。新建关联重写了页面来适应从课程接口进入的特点,显示界面中课程名称就是默认的了,需要管理员填入关联学院的id。
和从学院新建关联一样,两边的逻辑是完全一样的,因此重构了那边的代码,增加了对于输入的学院id的检查,并且利用表单post的便利,将本页面的path作为参数传递给了处理函数,使其根据不同的来源重定向到不同的位置,代码如下:
def create_course_association
has_that_dept = !Department.where("id=?",params[:course_department_association][:department]).empty?
has_that_course = !Course.where("id=?",params[:course_department_association][:course]).empty?
@redirect_path = params[:course_department_association][:path]
if !has_that_course
respond_to do |format|
format.html { redirect_to @redirect_path, notice: "No that course." }
end
elsif !has_that_dept
respond_to do |format|
format.html { redirect_to @redirect_path, notice: "No that department." }
end
else
@department = Department.find(params[:course_department_association][:department])
@course = Course.find(params[:course_department_association][:course])
has_that_ass = !CourseDepartmentAssociation.where("department_id = ? AND course_id =?" ,@department.id,@course.id).empty?
@department_course_relationship = @department.course_department_associations.new
@department_course_relationship.course = @course
respond_to do |format|
if has_that_ass
format.html { redirect_to @redirect_path, notice: "Has That Department Course Association." }
else
if @department_course_relationship.save
format.html { redirect_to @redirect_path, notice: "Department Course Association was successfully created." }
else
format.html { render :new }
end
end
end
end
end
不过这个页面的格式没太搞好,明天需要把后台的所有页面格式都优化一下。