Thursday, November 8, 2007

Degrading link_to_remote

I've been learning RubyOnRails and I noticed that the 'link_to_remote' helper didn't degrade gracefully...

Here's the fix I used (Inspired by this blog post).

(in helpers/application_helper.rb)

def link_to_remote(name, options = {}, html_options = {})
unless html_options[:href]
html_options[:href] = url_for(options[:url])
end

link_to_function(name, remote_function(options),
html_options)
end

Just keep in mind that this uses a HTTP GET when javascript is disabled. A HTTP GET should not have side effects. I can't think of a good way to do a HTTP POST from a link (trigger a form-post) without using javascript.

2 comments:

Anonymous said...

mate this was brilliant exactly what i needed thanks heaps

James West said...

Just wanted to add my thanks for your post.
Was just what I was looking for and am trying it as I type :-)