How to replace default dialog boxes in Rails 3 with jQuery UI Dialog?
-
We are a startup working on a saas product and we are using Rails 3 and JQuery as our technology. We need help to replace default confirm dialog boxes in rails.ujs with JQuery UI Dialog.
-
Answer:
It's actually pretty simple to overwrite the internal confirm method of rails.js. If you look into the rails.js code on Github: https://github.com/rails/jquery-ujs/blob/master/src/rails.js you will find the following line: // Default confirm dialog, may be overridden with custom confirm dialog in // $.rails.confirm confirm: function(message) { return confirm(message); } You could now either edit the file directly - which I highly recommend against - or go ahead and do what the comment says - implement $.rails.confirm. The problem now that you might have with jQuery UI is that you need to return true or false from the confirm function. Meaning that you need to stop everything inside that function until the user clicks abort (false) or ok (true). An example for using the $.rails JS methods with an async jQuery UI dialog could be something like this: // I'm only selecting delete links here $('a[data-method="delete"]').click(function(e) { // if the click event still triggers the rails behaviour as well // you could do this: // $('a[...]').unbind('click').click(function(e) { // and unbind all click events before. var link = $(this); // given you already have a dialog somewhere $( "#dialog-confirm" ).dialog({ resizable: false, height:140, modal: true, buttons: { "Delete all items": function() { // this should submit the link that has been clicked // if I'm not mistaken $.rails.handleMethod(link); // link has been defined above, // and we pass it to the rails ajax // handler $( this ).dialog( "close" ); // close the dialog }, Cancel: function() { $( this ).dialog( "close" ); // do nothing and close } } }); // discard the link's default behaviour e.preventDefault(); });
Max Schulze at Quora Visit the source
Other answers
I tried Max's solution but the default confirm() still fired (even with the unbind call) and using handleMethod() stopped my ajax from working. I found another solution that addressed the blocking issue here: http://stackoverflow.com/questions/4421072/jquery-ui-dialog-instead-of-alert-for-rails-3-data-confirm-attribute#6467532
John Mertens
Related Q & A:
- How to open the dialog box dynamically in iOS?Best solution by Stack Overflow
- Is there email validation baked in to Rails 3?Best solution by coverhound.com
- How to do android dialog flip animation?Best solution by developer.android.com
- how to add a form with a unique id on jquery?Best solution by Stack Overflow
- How do you jailbreak your iPod touch version 3.1.3?Best solution by wikihow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.