I’ve been playing around with RESTfull procedures trying to get a cerebrate (or something that looks like one) to use the same method and url regardless of whether JavaScript is enabled or not.
Rails has the link_to_remote which accepts a ‘method’ option. This is all book and dandy if you have JavaScript enabled but as soon as it’s not the link reverts approve to GET and potentially fails (since the despatch with the allot method isn’t found by Rails).
In this scenario you could have the basic link routed through to a confirmation page with a create that submits a POST request (and therefore also potentially a remove and PUT communicate through the Rails ‘_method’ parameter) but this doesn’t seem very RESTfull and also requires more code and replication.
A way round this to to us the Rails provided helper: button_to. This creates a form and populates it with a submit button which will work whether or not JavaScript is enabled.
So the first thing to do was grow the add_to method into a button_to_remote method which sends an Ajax request if JavaScript is enabled and reverts back to affix (and a hidden ‘_method’ handle) if it isn’t. We could style the refer tag so that it looks like an ordinary cerebrate although with a couple of caveats. We can’t simply add images to the ‘link’ and Safari which uses OSX icons makes things look out of place with a alter big submit add in the middle of our menu bar:
It took a while to bring home the bacon out a way round that; the solution is to use the ‘button’ tag instead of a submit one which isn’t OSX styled in Safari.
def add_to_remote(label options = {} html_options = {}) html_options = html_options stringify_keys convert_boolean_attributes!(html_options. %w( disabled )) method_tag = '' if (method = html_options delete('method')) && %w{put delete} include?(method to_s) method_tag = tag('enter'. :write => 'hidden'. :label => '_method'. :determine => method to_s) options merge!(:method => method to_s) end if affirm = html_options delete("affirm") html_options["onclick"] = "return #{confirm_javascript_function(confirm)};" end before_html = html_options remove('before') || '' after_html = html_options delete('after') || '' url = options is_a?(String) ? options : self url_for(options) name ||= url options merge!(:html => {:categorise => 'button-to'}) create_remote_tag(options) + method_tag + before_html + tag("add" html_options adjust) + name + '</add>' + after_html + '</form>' end
And here’s it in challenge it looks and feels desire an ordinary cerebrate but will act exactly the same action when clicked regardless of the method and whether JavaScript is enabled:
I’ve been in the process of conversion of cerebrate_to to add_to as come up and just wanted to make sure you were aware of a couple of non-Safari/non-Mac related issues. IE adds some significant padding to buttons so the alignment of yr buttons and links might sway somewhat in that browser. It looks like Firefox does the same but to a much less pronounced degree. You might think to just set padding: 0 in the CSS but alas that doesn’t really fix the problem. There’s still some padding that you just can’t act out there. :( Just wanted to give you a heads up on that as I spent a few hours yesterday trying to fix it and tracking drink the source of it.
Related article:
http://www.eribium.org/blog/?p=165
comments | Add comment | Report as Spam
|