Rails + jQuery and Links


#1

Hi,

I currently have a “save for later” function that effectively allows a
logged in user to save an position for later viewing. (Similar to the
watch/unwatch function on github). In my scenario I have a Position
model. Saved positions are resources that belong to the Position,
using the current_user before creation or deletion to create that user
association.

To do this I am doing a POST directly using the link:

<%= link_to(‘Save for Later’,
position_saved_position_url(@position),
:method => :post, :class => “new_saved_position”)-%>

However, in my application.js my code seems to still submit the link
and ignore the “return false;” at the end.

$(’.new_saved_position’).click(function(){
$.ajax({
type: “POST”,
url: $(this).href,
dataType: “script”,
beforeSend: function(xhr) {xhr.setRequestHeader(“Accept”, “text/
javascript”);}
});
return false;
});

My question:

By adding “:method => :post” am I altering/tainting the way this
should be done? I realize that having a direct POST like this is not
Restful - are there better ways of doing this? While not the topic of
this board - is my jQuery code wrong?

Thanks.