Rails Gracefully Degrading Javascript link_to for post / non-get methods

This works at least on Rails r8440. Just stick the code somewhere it gets loaded - I put it in vendor/plugins/graceful_link_to/init.rb

# graceful_link_to
# Jason Ling
# http://jason.lah.cc
#
# This hacks link_to to produce an alternative button_to that shows for users without javascript
# You need to include the prototype libraries and put at the bottom of your pages the following javascript
# javascript_tag "$$('.js_hide').invoke('hide'); $$('.js_show').invoke('show');"
ActionView::Helpers::UrlHelper.class_eval do
  def link_to_with_graceful(name, options = {}, html_options = {})
    return link_to_without_graceful(name, options, html_options) if !html_options[:method] || html_options[:method] == :get
    %Q(<div class="js_hide">#{button_to(name, options, html_options)}</div><span style="display: none;" class="js_show">#{link_to_without_graceful(name, options, html_options)}</span>)
  end
  alias_method_chain :link_to, :graceful
end

Leave a Reply