From 26a0bc14ebbc15024f52b9eb62c3c95502b6e928 Mon Sep 17 00:00:00 2001 From: stormand <422171566@qq.com> Date: Wed, 9 Nov 2016 01:08:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E6=AF=8F=E4=B8=AA=E8=AF=BE=E7=A8=8B?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E5=9B=BE=E7=89=87=20=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E5=8F=AF=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 460 +++++++++--------- Gemfile | 3 + Gemfile.lock | 12 +- app/assets/javascripts/image.coffee | 3 + app/assets/stylesheets/image.scss | 3 + app/controllers/courses_controller.rb | 3 +- app/models/course.rb | 2 + app/uploaders/avatar_uploader.rb | 51 ++ app/views/courses/_form.html.erb | 13 + app/views/courses/detail.erb | 1 + app/views/homes/index.html.erb | 3 +- .../20161108164630_add_avatar_to_courses.rb | 5 + db/schema.rb | 16 +- public/uploads/course/avatar/1/aaa.png | Bin 0 -> 243473 bytes 14 files changed, 341 insertions(+), 234 deletions(-) create mode 100644 app/assets/javascripts/image.coffee create mode 100644 app/assets/stylesheets/image.scss create mode 100644 app/uploaders/avatar_uploader.rb create mode 100644 db/migrate/20161108164630_add_avatar_to_courses.rb create mode 100644 public/uploads/course/avatar/1/aaa.png diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0e8ec16..fa0a4c2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,24 @@ + + + + + + + + + + + + + + + + @@ -32,21 +48,31 @@ - - + + - - + + - - + + - - + + + + + + + + + + + + @@ -55,18 +81,48 @@ - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -75,38 +131,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -122,16 +148,6 @@ - - - - - - - - - - @@ -148,12 +164,6 @@ @@ -265,6 +281,38 @@ - @@ -613,15 +669,15 @@ - + - + - + - + @@ -656,58 +712,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -795,14 +799,6 @@ - - - - - - - - @@ -854,22 +850,6 @@ - - - - - - - - - - - - - - - - @@ -889,31 +869,32 @@ - - + - - + + + + + + + + + + + + - + - - - - - - - - - - + + @@ -922,14 +903,41 @@ + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + @@ -942,58 +950,66 @@ - + - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Gemfile b/Gemfile index 2a375ba..46db05a 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,9 @@ gem 'sdoc', '~> 0.4.0', group: :doc # for postgresql gem 'pg', '~> 0.18.4' +gem 'carrierwave' +gem 'mini_magick' + # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server diff --git a/Gemfile.lock b/Gemfile.lock index c0a29c9..f3c45bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -47,6 +47,12 @@ GEM sass (>= 3.3.4) builder (3.2.2) byebug (9.0.5) + carrierwave (0.11.2) + activemodel (>= 3.2.0) + activesupport (>= 3.2.0) + json (>= 1.7) + mime-types (>= 1.16) + mimemagic (>= 0.3.0) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -87,6 +93,8 @@ GEM mime-types (3.1) mime-types-data (~> 3.2015) mime-types-data (3.2016.0521) + mimemagic (0.3.2) + mini_magick (4.5.1) mini_portile2 (2.1.0) minitest (5.9.0) multi_json (1.12.1) @@ -191,10 +199,12 @@ DEPENDENCIES bcrypt (~> 3.1.11) bootstrap-sass (~> 3.3.7) byebug + carrierwave coffee-rails (~> 4.1.0) faker (~> 1.6.3) jbuilder (~> 2.0) jquery-rails + mini_magick pg (~> 0.18.4) rails (= 4.2.5.2) rails_12factor @@ -207,4 +217,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.11.2 + 1.13.6 diff --git a/app/assets/javascripts/image.coffee b/app/assets/javascripts/image.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/image.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/image.scss b/app/assets/stylesheets/image.scss new file mode 100644 index 0000000..8032e75 --- /dev/null +++ b/app/assets/stylesheets/image.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the image controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 6bfe531..14f6bf6 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,5 +1,6 @@ class CoursesController < ApplicationController + before_action :student_logged_in, only: [:select, :quit, :list] before_action :teacher_logged_in, only: [:new, :create, :edit, :destroy, :update] before_action :logged_in, only: [:index] @@ -104,7 +105,7 @@ class CoursesController < ApplicationController def course_params - params.require(:course).permit(:course_code, :name, :course_type, :teaching_type, :exam_type, + params.require(:course).permit(:course_code, :avatar, :name, :course_type, :teaching_type, :exam_type, :credit, :limit_num, :class_room, :course_time, :course_week, :course_introduction) end diff --git a/app/models/course.rb b/app/models/course.rb index 45bb8e3..f909956 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -1,5 +1,7 @@ class Course < ActiveRecord::Base + mount_uploader :avatar, AvatarUploader + has_many :grades has_many :users, through: :grades diff --git a/app/uploaders/avatar_uploader.rb b/app/uploaders/avatar_uploader.rb new file mode 100644 index 0000000..31fb5a4 --- /dev/null +++ b/app/uploaders/avatar_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class AvatarUploader < 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/_form.html.erb b/app/views/courses/_form.html.erb index 4bc09c0..0b94a59 100644 --- a/app/views/courses/_form.html.erb +++ b/app/views/courses/_form.html.erb @@ -102,6 +102,19 @@ +
+ <%= f.label "图片", class: 'col-sm-3 control-label' %> +
+
+
+ <%= f.file_field :avatar %> + <%= f.hidden_field :avatar_cache %> +
+
+
+ + + <%= f.submit '提交', class: "btn btn-success btn-block" %> diff --git a/app/views/courses/detail.erb b/app/views/courses/detail.erb index e4ee1d9..4d2ea51 100644 --- a/app/views/courses/detail.erb +++ b/app/views/courses/detail.erb @@ -15,6 +15,7 @@
<%= @course.course_introduction %> + <%= image_tag @course.avatar.to_s %>
diff --git a/app/views/homes/index.html.erb b/app/views/homes/index.html.erb index 648a97b..4afdb82 100644 --- a/app/views/homes/index.html.erb +++ b/app/views/homes/index.html.erb @@ -10,7 +10,7 @@