Skip to content

Instantly share code, notes, and snippets.

@stan
Forked from vrinek/gist:451508
Created January 13, 2011 04:25
Show Gist options
  • Save stan/777387 to your computer and use it in GitHub Desktop.
Save stan/777387 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