Skip to content

Instantly share code, notes, and snippets.

@victusfate
Forked from stan/gist:777387
Created May 18, 2011 21:46
Show Gist options
  • Save victusfate/979643 to your computer and use it in GitHub Desktop.
Save victusfate/979643 to your computer and use it in GitHub Desktop.
// http://stackoverflow.com/questions/3503660/upgrading-with-parameter-on-a-link-to-remote-in-rails-3
// http://railsforum.com/viewtopic.php?id=42215
// http://stackoverflow.com/questions/2508213/rails3-link-to-with-attribute
$$('a[data-remote=true]').each(function(a){
a.onclick = function(){
href = a.getAttribute('href');
url = href.split('?')[0];
params = href.split('?')[1];
newParam = encodeURI(eval(a.getAttribute('data-with')));
if(params){
params = params.split('&');
params.each(function(p){
if(p.match(new RegExp('^'+newParam.split('=')[0]+'='))){
oldParam = p;
};
});
params = params.without(oldParam);
}else{
params = [];
};
params.push(newParam);
a.setAttribute('href', url + '?' + params.join('&'));
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment