modify library

dev_aliyun
p31729568 5 years ago
parent 9a3191d1ce
commit 724dce7803

@ -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

@ -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

@ -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

@ -1,6 +1,6 @@
<p class="lineh-25 font-22 mb20">上传教学案例</p>
<div class="library-form-container">
<%= form_for(@library) do |f| %>
<%= form_for(@library, html: { multipart: true }) do |f| %>
<% tag_ids = @library.library_tags.map(&:id) %>
<%= hidden_field_tag :apply_publish, false %>
<%= hidden_field_tag :tag_ids, tag_ids.join(',') %>
@ -20,12 +20,12 @@
<div class="clearfix mt20">
<span class="upload_Title">作者</span>
<li class="fl">
<input class="greyInput winput-300-35 mr20 fl winput150" id="library_title" name="library[author_name]" placeholder="请输入姓名" size="30" type="text" width="163">
<p style="height:20px;line-height:20px;"><span class="color-red none" id="title_notice">请输入姓名</span></p>
<%= f.text_field :author_name, placeholder: '请输入姓名', class: 'greyInput winput-300-35 mr20 fl winput150', width: '163', size: 30 %>
<p style="height:20px;line-height:20px;"><span class="color-red none" id="author_name_notice">请输入姓名</span></p>
</li>
<li class="fl">
<input class="greyInput winput-300-35 mr20 fl" id="library_title" name="library[author_school_name]" placeholder="请输入作者单位名称" size="30" type="text">
<p style="height:20px;line-height:20px;"><span class="color-red none" id="title_notice">请输入作者单位名称</span></p>
<%= f.text_field :author_school_name, placeholder: '请输入作者单位名称', class: 'greyInput winput-300-35 mr20 fl', width: '163', size: 30 %>
<p style="height:20px;line-height:20px;"><span class="color-red none" id="author_school_name_notice">请输入作者单位名称</span></p>
</li>
</div>
@ -61,38 +61,22 @@
<p style="height:22px;line-height:22px;"><span class="color-red none" id="file_notice">请上传附件</span></p>
</div>
<div class="clearfix lineheight35">
<% cover_exists = @library.cover_id.present? %>
<% cover_exists = @library.cover_id.present? && !@library.cover_id_changed? %>
<div><span class="upload_Titles">封面图</span> <span class="color-grey-c">上传尺寸120*90 px</span></div>
<div class="surfacePlot ml25">
<img src="/images/educoder/unite.png" class="marginuploading"/>
<%= hidden_field_tag 'library[cover_id]', @library.cover_id %>
<%= hidden_field_tag 'cover_file_name', '', class: 'cover-file-name' %>
<input type="file" name="cover_file" accept="image/*" id="library-cover-file" style="display: none"/>
<div class="surfacePlot ml25 library-cover-select" data-tip-down="上传图片" onclick="$('#library-cover-file').click();">
<img src="/images/educoder/unite.png" class="marginuploading <%= cover_exists ? 'hidden' : '' %>" style="<%= cover_exists ? 'display:none' : '' %>"/>
<div class="library-cover-perview" style="<%= cover_exists ? '' : 'display:none' %>">
<% cover_url = cover_exists ? named_attachment_path(@library.cover.id, @library.cover.filename) : '' %>
<img src="<%= cover_url %>" width="120px" height="90px"/>
</div>
<% if cover_exists %>
<%= image_tag(named_attachment_path(@library.cover.id, @library.cover.filename), :class => "w120 h90 ml5 shixun_image_show") %>
<% else %>
<img src="" class="ml5 shixun_image_show ml50 none" width="150px" height="150px" id="shixun_image_show_<%= @library.id %>"/>
<% end %>
<%= file_field_tag 'library[cover_id]',
:id => "upload_img_#{@library.id}",
:style => 'display:none;',
:size => "1",
:multiple => false,
:onchange => 'addInputAvatar(this);',
:data => {
:max_file_size => Setting.authentication_img_max_size.to_i.kilobytes,
:max_file_size_message => l(:error_user_auth_too_big, :max_size => number_to_human_size(Setting.authentication_img_max_size.to_i.kilobytes)),
:max_concurrent_uploads => Redmine::Configuration['max_concurrent_ajax_uploads'].to_i,
:file_type => Redmine::Configuration['pic_types'].to_s,
:type_support_message => l(:error_pic_type),
:upload_path => uploads_path(:format => 'js',:project =>nil),
:description_placeholder => nil ,# l(:label_optional_description)
} %>
<div>
<a href="javascript:void(0);" id="object_upload_img_<%= @library.id %>" class="<%= cover_exists ? "mr10 ml60" : "mr10 ml60 decoration4CACFF" %>" onclick="$('#upload_img_<%= @library.id %>').click();">
<%= cover_exists ? "重新上传" : "上传图片" %>
</a>
</div>
<p style="height:20px;line-height:20px;"><span class="color-red ml20 none" id="cover_file_notice">请上传封面</span></p>
</div>
</div>
<div class="padding30 bor-top-greyE">
@ -155,6 +139,9 @@
var submitForm = function(){
var title = $("input[name='library[title]']").val();
var content = $("textarea[name='library[content]']").val();
var author_name = $("input[name='library[author_name]']").val();
var author_school_name = $("input[name='library[author_school_name]']").val();
var cover_file = $("input[name='cover_file").val();
if (!title || title.length == 0) {
$("#title_notice").removeClass("none");
@ -168,6 +155,25 @@
}else{
$("#des_notice").addClass("none");
}
if (!author_name || author_name.length == 0) {
$("#author_name_notice").removeClass("none");
return
}else{
$("#author_name_notice").addClass("none");
}
if (!author_school_name || author_school_name.length == 0) {
$("#author_school_name_notice").removeClass("none");
return
}else{
$("#author_school_name_notice").addClass("none");
}
var coverId = $("input[name='library[cover_id]']").val();
if ((!coverId || coverId.length == 0) && (!cover_file || cover_file.length == 0)) {
$("#cover_file_notice").removeClass("none");
return
}else{
$("#cover_file_notice").addClass("none");
}
if($('.attachments_fields .attachment').length == 0){
$("#file_notice").removeClass("none");
@ -199,5 +205,26 @@
console.log('ids', ids)
$('#tag_ids').val(ids);
})
$("#library-cover-file").change(function (e) {
var file = e.target.files[0] || e.dataTransfer.files[0];
$('input.cover-file-name').val(document.getElementById("library-cover-file").files[0].name);
if (file) {
var reader = new FileReader();
reader.onload = function () {
$(".library-cover-perview img").attr("src", this.result);
$('.marginuploading').addClass('hidden').hide();
$('.library-cover-perview').show();
}
reader.readAsDataURL(file);
}
});
$('.library-cover-select').hover(function(){
$('.library-cover-select .marginuploading.hidden').show();
},function(){
$('.library-cover-select .marginuploading.hidden').hide();
});
});
</script>

@ -1211,18 +1211,18 @@ html>body #ajax-indicator { position: fixed; }
}
.surfacePlot{
width:120px;
height:90px;
cursor: pointer;
width:122px;
height:92px;
background:rgba(250,250,250,1);
border:1px solid rgba(221,221,221,1);
}
.marginuploading{
margin: 27px 42px;
}
.roundedRectangles{
padding: 28px 43px;
position: absolute;
top: 10px;
right: -22px;
z-index: 10;
}
.surfacePlot:hover .marginuploading.hidden{
background: rgba(0,0,0, 0.1);
}
Loading…
Cancel
Save