From fe4b834b964db7690730cfdbfa709f8b51c2e6d5 Mon Sep 17 00:00:00 2001 From: stormand <422171566@qq.com> Date: Thu, 5 Jan 2017 16:46:03 +0800 Subject: [PATCH] t --- app/controllers/courses_controller.rb | 14 +++++ app/models/course.rb | 1 + app/uploaders/excel_uploader.rb | 51 +++++++++++++++++++ app/views/courses/index.html.erb | 6 +++ config/routes.rb | 3 ++ .../20170105083346_add_newfile_to_course.rb | 5 ++ 6 files changed, 80 insertions(+) create mode 100644 app/uploaders/excel_uploader.rb create mode 100644 db/migrate/20170105083346_add_newfile_to_course.rb diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 1418e96..2f30828 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -57,6 +57,20 @@ class CoursesController < ApplicationController redirect_to courses_path, flash: {:success => "已经成功关闭该课程:#{ @course.name}"} end + def excel + @course= current_user.teaching_courses + end + + def excel_in + @course = Course.find_by_id(params[:id]) + if @course.update_attributes(course_params) + flash={:info => " 导入成功"} + else + flash={:warning => "导入失败"} + end + redirect_to excel_courses_path, flash: flash + end + #-------------------------for students---------------------- def list diff --git a/app/models/course.rb b/app/models/course.rb index f909956..038e9dc 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,6 +1,7 @@ class Course < ActiveRecord::Base mount_uploader :avatar, AvatarUploader + mount_uploader :excel, ExcelUploader has_many :grades has_many :users, through: :grades diff --git a/app/uploaders/excel_uploader.rb b/app/uploaders/excel_uploader.rb new file mode 100644 index 0000000..bba3ed9 --- /dev/null +++ b/app/uploaders/excel_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class ExcelUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + # include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :resize_to_fit => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + # def extension_white_list + # %w(jpg jpeg gif png) + # end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/courses/index.html.erb b/app/views/courses/index.html.erb index be09b68..55a5a02 100644 --- a/app/views/courses/index.html.erb +++ b/app/views/courses/index.html.erb @@ -28,6 +28,7 @@ 编辑课程 打开课程 删除课程 + 上传最新资料 <% elsif student_logged_in? %> 删除课程 是否选为学位课 @@ -52,6 +53,11 @@ <%= link_to "打开", open_course_url(course), class: 'btn btn-xs btn-info' %> <% end %> <%= link_to "删除", course_path(course), :method => "delete", class: 'btn btn-xs btn-danger', :data => {confirm: '确定要删除此课程?'} %> + + <%= f.file_field :excel %> + <%= f.hidden_field :excel_cache %> + + <% elsif student_logged_in? %> <%= link_to "删除", quit_course_path(course), class: 'btn-sm btn-danger' %> <% if course.degree %> diff --git a/config/routes.rb b/config/routes.rb index be031c5..fd8bddd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,12 +27,15 @@ Rails.application.routes.draw do get :classtable get :isdegree #add route isdegree get :nodegree #add route nodegree + patch :excel_in + patch :excel_out end collection do get :list get :search get :refresh_search get :credit #add credit route + get :excel end end diff --git a/db/migrate/20170105083346_add_newfile_to_course.rb b/db/migrate/20170105083346_add_newfile_to_course.rb new file mode 100644 index 0000000..a17bb9b --- /dev/null +++ b/db/migrate/20170105083346_add_newfile_to_course.rb @@ -0,0 +1,5 @@ +class AddNewfileToCourse < ActiveRecord::Migration + def change + add_column :courses, :excel, :string + end +end