从EditorGrid面板删除/修改数据库记录

时间:2011-04-18 12:56:22

标签: ruby-on-rails ajax extjs

我在Rails 3.x中使用ExtJs。如何使用Ext.Ajax.request方法从EditorGrid 删除/修改记录?

我尝试使用store.remove()和类似的其他方法,但它不会从数据库中删除记录。

提前致谢!!

代码:

    //** Destroy method **//

         def destroy
            puts 'destroy'
            @unit = Unit.find(params[:id])
            puts 'will destroy'
            @unit.destroy
            puts 'destroyed'
         end

    //** units.js **//

    var delbtn = Ext.getCmp('btnDelete');
                 delbtn.on('click',function(){

                     var grid = Ext.getCmp('maingrid');
                     var selection = grid.getSelectionModel().getSelected();
                     Ext.Ajax.request({
                     url: '/units/destroy',
                     method: 'POST',
                     params: {
                     'id' : selection.data.id }

                    });

                });//end del function

//** Store.js **//
Ext.data.Api.restActions = {
            create  : 'POST',
            read    : 'GET',
            update  : 'PUT',
            destroy : 'DELETE' };
storeId: 'MyUnitStore',
            root: 'data',
            autoLoad: true,
            autoSave: false,
            //batch: true,
            restful:true,
            writer: new Ext.data.JsonWriter({
                encode : false,
                listful:false,

                destroy: '/units/destroy',
                update : '/edit'
            }),
             url: '/units.json',
                fields: [
                {
                    name: 'unitname'
                },
                {
                    name: 'description'
                }
                 ]

//** Edit Button **//
var store = Ext.getCmp('maingrid').getStore();                 
                 store.update();

1 个答案:

答案 0 :(得分:1)

store.remove()仅从网格的关联商店中删除数据(记录)。它本身不与服务器端通信。您需要使用Ajax请求并与Rails 3.x通信以删除相应的记录。

var selection = grid.getSelectionModel().getSelected();
Ext.Ajax.request({
    url: '/ExampleController/Delete',
    method: 'POST',
    params: {
        'id': selection.data.id
    },
    success: function(result, request){
        // Refresh the grid store and display
    },
    failure: function(result, request){
        // Display delete error message
    }
}