Gem for Rails that creates a convenient scaffold using Ajax in an unobtrusive way usign jQuery and jQueryUI.
This gem has been tested working on Rails 3.
-
jQuery
-
jQuery_ujs
-
jQueryUI (including stylesheet and images)
Add the gem to your Gemfile then run bundle
to install it.
gem 'ajaxcrud'
Install jQuery and jQueryUI(js and css) using assets on Rails 3.1 by adding the next line to app/assets/javascripts/application.js
# app/assets/javascripts/application.js //= require jquery //= require jquery_ujs //= require jquery-ui
And remember to also add the stylesheet and images included on jQueryUI to the app/assets/stylesheets/ folder.
Theres only one command to run the generator and create a scaffold, and goes as follows:
rails g ajaxcrud NAME [field:type:boolean field:type:boolean]
Where NAME
is the name of the model you are going to create. Additional to that field
is the name of the attribute you are going to add and type
is the type of field, just as a regular scaffold. The extra boolean
in there is to indicate if that attribute is going to be indexed in the search field, it can be true
, false
or blank
being false as default.
For example, if we are going to create a scaffold for Person
which will include attributes name
, address
, website
and phone_number
being name
and website
the only attributes that are going to be indexed in the search field, we should run the following command:
rails g ajaxcrud Person name:string:true address:text website:string:true phone_number:string
This will create the Ajax scaffold and additional to that a general stylesheet to give some default color to the index table, an ajaxcrud helper to make index.html.erb
more self-explanatory.
You can configurate the default table to show more attributes that the one’s indexed, just edit the show.html.erb
file.
Helpers that are going to be added on index.html.erb
are:
-
ajaxcrud_js(name, new_text, info_text) post a javascript required code inside your index file. Where
name
is the name of your model which in our example isperson
,new_text
would be the text to show in the dialog box when a user is going to be created and alsoinfo_text
works in a similar way but for the show dialog. -
ajaxcrud_dialogbox(name) is used to post the code that build the dialog boxes on the file. Where
name
is the name of your model which in our example isperson
. -
ajaxcrud_notices(name) is included in the place where notices will be shown in a
flash[:notice]
style but with some different finish. Wherename
is the name of your model which in our example isperson
. -
ajaxcrud_search(name) is the search field used to look for in the table records. Where
name
is the name of your model which in our example isperson
. -
ajaxcrud_new_link(name) creates the link to enable the New Record dialog. Where
name
is the name of your model which in our example isperson
. -
ajaxcrud_show_link(path) creates the link to render the
show.html.erb
file of the selected record inside a dialog. Wherepath
is the original way to access the show file.
-
When creating the same ajaxcrud twice the
migration
file is going to be created more than once. -
If generator
destroy
is used everything is deleted (including the helper,ajaxcrud.css
,…) of the model you indicate EXCEPT the migration file which needs to be removed manually. -
When destroying a model the
ajaxcrud.css
andajaxcrud_helper.rb
are also deleted. Since they are being used for all the scaffolds generated you can add them manually and remember to add +include AjaxcrudHelper+ to your app/helpers/application_helper.rb file.
This gem was made based on some of the tutorial made by ‘Nikunj’ at the {Tech Journey}[http://codefundas.blogspot.com/2010/12/create-ajax-based-curd-using-rails-3.html]‘s website and being adapted to change or add some functionalities to that.
Also thanks to Ryan Bates who helped me find out how to create my first generator and gem, and whose website is awesome and has helped me a lot learning how to code in Ruby on Rails. Highly Recomended.