class SchoolController < ApplicationController layout 'course_base' before_filter :require_admin, :only => :upload_logo def upload uploaded_io = params[:logo] school_id ||= params[:id] s1 = School.find(school_id) unless uploaded_io.nil? File.open(Rails.root.join('public', 'images', 'school', school_id.to_s+'.png'), 'wb') do |file| file.write(uploaded_io.read) end s1.logo_link = '/images/school/'+school_id.to_s+'.png' end s1.name = params[:name] unless params[:name].blank? s1.province = params[:province] unless params[:province].blank? s1.save redirect_to admin_schools_url(:school_name => params[:school_name]) end def upload_logo @school = School.find params[:id] @school_name = params[:school_name] end #获取制定学校开设的课程数 def course_count school_id School.find(school_id).courses.count end def index end def get_province @provinces = School.find_by_sql("select distinct province from schools") options = "" @provinces.each do |p| options << "" end # 取id取学校名 # 连接子表: 查询已添加用户的学校 school = School.select("id, name"). joins("RIGHT JOIN ( SELECT DISTINCT school_id FROM #{UserExtensions.table_name} WHERE school_id IS NOT NULL) AS sids ON schools.id = sids.school_id"). where("#{School.table_name}.id IS NOT NULL") options_s = "" school.each do |s| options_s << "
  • #{s.name}
  • " end res = Hash.new res[:text] = options res[:text_s] = options_s render :json => res end def get_options @school = School.where("province = ?", params[:province]) p = params[:province] ##@school = School.all options = "" @school.each do |s| #options << "" options << "
  • #{s.name}
  • " end render :text => options end def get_schoollist @school = School.where("province = ?", params[:province]) options = "" @school.each do |s| options << "
  • #{s.name}
  • " end render :text => options end def search_school q = "%#{params[:key_word].strip}%" @school = School.where("name LIKE ?", q) @school = @school.where("province = ?", params[:province]) if (params[:province] != '0' ) options = "" @school.each do |s| options << "
  • #{s.name}
  • " end options = "
    #{l(:label_school_not_fount)}
    " if options.blank? render :text => options end #根据学校名字或者拼音来查询 def on_search condition = "#{params[:name].strip}".gsub(" ","") #将条件截断为汉字和拼音(全汉字 或者 全拼音 或者 汉字和拼音), #获取拼音的第一次出现的位置 chinese = [] pinyin = [] condition.scan(/./).each_with_index do |char,index| if char =~ /[a-zA-Z0-9]/ pinyin << char else chinese << char end end if(condition == '') @school = School.page((params[:page].to_i || 1) - 1).per(100) @school_count = School.count else @school = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").page((params[:page].to_i || 1) - 1).per(100) @school_count = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").count end result = [] # @school.each do |sc| # result << {:value=>sc.name,:data=>sc.id} # end render :json =>{ :schools => @school,:count=>@school_count}.to_json end #添加学校 def add_school @school = School.new @school.name = params[:name].strip @school.pinyin = Pinyin.t(params[:name].strip, splitter: '') @school.save respond_to do |format| format.js end end end