diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index b0a7be31..e41e2a43 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -359,6 +359,7 @@ class AttachmentsController < ApplicationController saved = @attachment.save respond_to do |format| format.js + format.json { render json: { attachment_id: @attachment.id, url: download_attachment_path(@attachment.id) } } format.api { if saved render :action => 'upload', :status => :created diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index db5d20b0..b7bbf836 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -8,7 +8,7 @@ class LibrariesController < ApplicationController libraries = Library.where(nil) libraries = - if current_user.logged? && params[:type] == 'mine' + if User.current.logged? && params[:type] == 'mine' libraries.where(user_id: current_user.id).order('created_at desc') else libraries.where(status: :published).order('visited_count desc') @@ -49,7 +49,7 @@ class LibrariesController < ApplicationController rescue ActiveRecord::RecordInvalid => e flash[:message] = e.record.errors.full_messages.join(',') render 'new' - rescue Libraries::SubmitService::Error => ex + rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex flash[:message] = ex.message render 'new' end @@ -73,7 +73,7 @@ class LibrariesController < ApplicationController rescue ActiveRecord::RecordInvalid => e flash[:message] = e.record.errors.full_messages.join(',') render 'edit' - rescue Libraries::SubmitService::Error => ex + rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex flash[:message] = ex.message render 'edit' end @@ -117,6 +117,7 @@ class LibrariesController < ApplicationController hash = params[:library].presence || {} hash[:tag_ids] = params[:tag_ids].to_s.split(',') hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]} + hash[:cover_id] = save_cover.try(:id) || hash[:cover_id] hash end end @@ -132,4 +133,14 @@ class LibrariesController < ApplicationController def increment_visit_count @library.increment_visited_count! if @library && @library.id end + + def save_cover + return if params[:cover_file].blank? + + attachment = Attachment.new(file: params[:cover_file]) + attachment.author = User.current + attachment.filename = Redmine::Utils.random_hex(16) + attachment.save + attachment + end end \ No newline at end of file diff --git a/app/models/library.rb b/app/models/library.rb index c0fe35ee..6029f984 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -17,7 +17,7 @@ class Library < ActiveRecord::Base validates :content, presence: true validates :uuid, presence: true, uniqueness: true validates :author_name, presence: true, length: { maximum: 10 } - validates :author_school_name, length: { maximum: 50 }, allow_blank: true + validates :author_school_name, presence: true, length: { maximum: 50 } acts_as_attachable diff --git a/app/views/competitions/_header.html.erb b/app/views/competitions/_header.html.erb index 18e284ec..698238cb 100644 --- a/app/views/competitions/_header.html.erb +++ b/app/views/competitions/_header.html.erb @@ -64,11 +64,30 @@
- <% if User.current.logged? %> + \ No newline at end of file diff --git a/app/views/libraries/index.html.erb b/app/views/libraries/index.html.erb index 08495304..087ae03c 100644 --- a/app/views/libraries/index.html.erb +++ b/app/views/libraries/index.html.erb @@ -6,16 +6,14 @@ <%= link_to '发布案例', new_library_path, class: 'white-btn edu-filter-btn-blue fr mr10 mt8', style: 'color: #4CACFF' %>