Browse DevX
Sign up for e-mail newsletters from DevX


Combine jqGrid and Rails for Beautified Business Applications : Page 3

Learn how to build a rich, data-driven Rails application by adding a slick jQuery grid (jqGrid) to it.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

CRUD Params from jqGrid to Rails Controller

The params to the controllers in any Rails app are typically held in the params map. However, unlike the controller/method convention that Rails uses, the parameters related to CRUD operations from jqGrid are specific values and passed through the params map.

def post_data if params[:oper] == "del" ItemInstance.find(params[:id]).destroy else @item_instance = { :id => params[:id], :ITEM_METADATA_ID => params[:q], :NAME => params[:NAME], :DISPLAY_NAME => params[:DISPLAY_NAME] } if params[:id] == "_empty" ItemInstance.create(@item_instance) else ItemInstance.find(params[:q]).update_attributes(@item_instance) end end render :nothing => true end

The creation (addition) of a new row in the table is indicated by the "params[:id] == "_empty"" parameter. In case the row is already present and you have edited the row, then the "params[:id]" will contain an integer value. The deletion (removal) of a row is indicated by the "params[:oper] == "del"" field. In the case of delete operation, the "params[:id] " field will also contain the id of the row that you have selected for deletion. The method post_data within the controller actually acts as an interface to actual operations, and it overrides the Rails' conventional method of controller/create for creation, controller/destroy for deletion, etc. However, it is still a good idea to use post_data as a wrapper to the actual method in the convention hierarchy. This ensures that the convention is not broken. This is done by passing the data to the respective method as shown below:

def post_data if params[:oper] == "del" destroy else if params[:id] == "_empty" create else ItemInstance.find(params[:q]).update_attributes(@item_instance) end end render :nothing => true end

Users may choose to delete multiple rows at the same time. In the case of multiple selections, the selected elements' ids are separated by a comma as shown below:

Def destroy ids = params[:id].split(","); @instances = ItemInstance.find(ids) if(@instances.nil?) puts "**********************" puts "No item found for deletion" puts "**********************" end @instance.each do |instance| instance.destroy end @instances = ItemInstance.find(:all, :limit => params[:rows], :offset => (params[:page].to_i - 1) * params[:rows].to_i); end

A read typically is handled using the find method of the Model. In this particular case, ItemInstance is the model and ItemInstancesController is the controller. One can also notice that the number of records that are fetched is controlled through the params[:rows] and params[:page] parameters. As shown in Figure 2, jqGrid also comes with a drop-down selector which can be used to control the number of rows that one would want to see in the table at a time. The values selected form the UI will determine the values of params[:rows] and params[:page] respectively.

This makes the story complete. With the definition of the method post_data, the integration of the CRUD that was talked about earlier becomes complete. The 'del' and 'add' which had been declared in navGrid of the view has now been connected to the post_data method of the ItemInstancesController. As a good practice, it is better to let the Rails methods create, update and destroy to the necessary CRUD operations and not let post_data do the same.

References and Acknowledgements

  1. I suggest the reader go through www.trirand.com for the great grid plug-in called jqGrid.
  2. I would also suggest the readers go through www.2dconcept.com for a great Rails plugin that makes our life much easier, while integrating Rails with jqGrid.
  3. It would be great to look into www.rubyonrails.org for any help related to installing Ruby on Rails, especially if you is a beginner.
  4. Exploring www.jquery.com and www.plugins.jquery.com will also be worthwhile.

Raveendran Vadakkoot is a software architect, designer and engineer with more than 13+ years of experience in multinational software industries.
Thanks for your registration, follow us on our social networks to keep up-to-date