Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

添加图片更新功能

  • Loading branch information...
commit 6378eec9fbfb80778d00a166a8d9b8c8cc1374af 1 parent 879ec01
mouseshi authored
17 app/controllers/students_controller.rb
@@ -18,9 +18,26 @@ def student_total_score
18 18 render_json Student.find(params[:s_id]).total_score_by_grade
19 19 end
20 20
  21 + #删除学生
21 22 def destroy_student
22 23 Student.find(params[:id]).delete
23 24 render_json "success"
24 25 end
25 26
  27 + #保存学生更新
  28 + def update_student
  29 + data = {
  30 + :number => params["number"],
  31 + :sex => params["sex"],
  32 + :phone => params["phone"],
  33 + :home => params["home"],
  34 + :name => params["name"],
  35 + }
  36 + data["image"] = params["photo"] if(params["photo"])
  37 + Student.find(params["id"]).update_attributes!(data)
  38 + render_json "success", "text/html"
  39 + rescue => e
  40 + render_error e.message,'text/html'
  41 + end
  42 +
26 43 end
5 app/models/person.rb
... ... @@ -1,4 +1,7 @@
1 1 class Person < ActiveRecord::Base
  2 + has_attached_file :image,
  3 + :default_url => "/images/Temp.png",
  4 + :styles => { :medium => "300x300>",
  5 + :thumb => "100x100>" }
2 6 belongs_to :classes
3   -
4 7 end
1  app/views/layouts/application.html.erb
@@ -44,6 +44,7 @@
44 44 )
45 45 }
46 46 );
  47 + Ext.Ajax.extraParams = { authenticity_token: "<%= form_authenticity_token %>" };
47 48 </script>
48 49 <div class="dock" id="dock2">
49 50 <div class="dock-container2">
1  config/routes.rb
@@ -21,6 +21,7 @@
21 21 get "get_all_students"
22 22 get "student_total_score"
23 23 post "destroy_student"
  24 + post "update_student"
24 25 end
25 26 member do
26 27 post "update_student_comment"
70 public/javascripts/PF/pages/settings_index.js
@@ -5,8 +5,9 @@ Pf.settings.homeIndex = {
5 5 var studentDetailFormPanel = this.createStudentDetail();
6 6 grid.store.load();
7 7 grid.on('cellclick', function(grid, rowIndex){
8   - studentDetailFormPanel.getForm().setValues(grid.store.getAt(rowIndex).data);
9   - $("#image img").attr("src", '/images/Temp.png');
  8 + var record = grid.store.getAt(rowIndex).data;
  9 + studentDetailFormPanel.getForm().setValues(record);
  10 + $("#image img").attr("src",record["image/url"] );
10 11 });
11 12
12 13 var panel = new Ext.TabPanel({
@@ -56,7 +57,6 @@ Pf.settings.homeIndex = {
56 57 createStudentDetail: function(){
57 58 var sexData = [['男','男'],['女','女']];
58 59 var sexCombo = new Ext.form.ComboBox({
59   - //id: 'targetCombo',
60 60 valueField: 'sex',
61 61 fieldLabel: "性别",
62 62 triggerAction: 'all',
@@ -68,19 +68,69 @@ Pf.settings.homeIndex = {
68 68 store: new Ext.data.SimpleStore({
69 69 fields: ['sex', 'sexName'],
70 70 data: sexData,
71   - //autoLoad: true,
72 71 })
73 72 });
  73 +
  74 + //保存更新按钮
  75 + function onSave(){
  76 + var grid = Ext.getCmp("studentShowGrid");
  77 + var record = grid.getSelectionModel().getSelected();
  78 + if(!record){ Ext.Msg.alert("提示","请选择学生") }
  79 + else{
  80 + Ext.Msg.confirm("提示","是否确定保存更改",function(button){
  81 + if(button != "no"){
  82 + Ext.getCmp("studentDetailFormPanel").getForm().submit({
  83 + clientValidation: true,
  84 + method: "POST",
  85 + waitMsg: "保存中",
  86 + params: { id: record.data.id },
  87 + url: "/students/update_student",
  88 + success: function(form,action){
  89 + Ext.Msg.alert('提示', "保存成功");
  90 + grid.store.on("load",function(){
  91 + var new_record = Ext.getCmp("studentShowGrid").getSelectionModel().getSelected();
  92 + if(new_record)
  93 + {
  94 + $("#image img").attr("src",new_record.data["image/url"] );
  95 + }
  96 + });
  97 + grid.store.reload();
  98 + Ext.getCmp("photoField").reset();
  99 + },
  100 + failure: function(){
  101 + Ext.Msg.alert('提示', "保存失败");
  102 + }
  103 + });
  104 + }
  105 + })
  106 + }
  107 + };
  108 +
  109 + //判断上传文件
  110 + function onFileSelect(field){
  111 + var value = field.getValue();
  112 + var pattern = /\.jpg$|\.jpeg$|\.bmp$|\.png|\.gif$|\.png&/i;
  113 + if (!pattern.test(value)){
  114 + Ext.Msg.show({
  115 + title:"警告",
  116 + msg:"必须是JPG,GIF,PNG,BMP格式图片文件!",
  117 + buttons: Ext.MessageBox.OK ,
  118 + icon: Ext.MessageBox.INFO
  119 + });
  120 + field.setValue("");
  121 + };
  122 + };
74 123
75 124 var formPanel = new Ext.form.FormPanel({
76 125 title: "test",
77 126 region: "center",
  127 + id: "studentDetailFormPanel",
78 128 autoScroll : true,
79 129 fileUpload: true,
80 130 frame: true,
81 131 labelAlign : 'right',
82 132 buttonAlign: 'center',
83   - buttons: [{ text: "更新", handler: function(){ alert("nihai") } }],
  133 + buttons: [{ text: "更新", handler: function(){ onSave() } }],
84 134 items: [{
85 135 layout: 'column',
86 136 items:[
@@ -115,8 +165,6 @@ Pf.settings.homeIndex = {
115 165 name: "classes/name"
116 166 },
117 167 sexCombo,
118   - // fieldLabel: "性别",
119   - // name: "sex"
120 168 {
121 169 fieldLabel: "联系电话",
122 170 name: "phone"
@@ -130,13 +178,13 @@ Pf.settings.homeIndex = {
130 178 xtype: 'fileuploadfield',
131 179 allowBlank : true,
132 180 emptyText: '选择',
  181 + id: "photoField",
133 182 fieldLabel: '头像',
134   - name: 'size_sheet',
  183 + name: 'photo',
135 184 buttonText: '上传',
136   - listeners: { 'fileselected' : {fn: this.onFileSelect, scope: this} }
  185 + listeners: { 'fileselected' : {fn: onFileSelect, scope: this} }
137 186 }
138   - ]
139   - }
  187 + ]}
140 188 ]
141 189 }]
142 190 });
3  public/javascripts/PF/pages/student.js
@@ -31,6 +31,7 @@ Pf.classes.student = Ext.extend(Ext.grid.EditorGridPanel,{
31 31 "phone",
32 32 "home",
33 33 "classes/name",
  34 + "image/url",
34 35 ],
35 36 root: 'root',
36 37 url: '/students/get_all_students.json',
@@ -67,7 +68,7 @@ Pf.classes.student = Ext.extend(Ext.grid.EditorGridPanel,{
67 68 var record = grid.getSelectionModel().getSelected();
68 69 if(!record){ Ext.Msg.alert("提示","请选择学生") }
69 70 else{
70   - Ext.Msg.confirm('提示', "是否确定保存?", function(button){
  71 + Ext.Msg.confirm('提示', "是否确定删除?", function(button){
71 72 if(button != "no"){
72 73 Pf.util.loadMask.show();
73 74 var studentId = record.id;

0 comments on commit 6378eec

Please sign in to comment.
Something went wrong with that request. Please try again.