In my layout, I have a devise sign in / sign out link, like so:
=if user_signed_in? then link_to "_", destroy_user_session_path, :method => :delete else link_to "_", new_user_session_path, :method => :get end
This uses the rails helpers to build up the link, and resolves to the following HTML:
<a data-method="get" href="/users/sign_in">_</a>
I'm converting all links to buttons, and have just passed in URLs to onClick functions to redirect the browser. In this case, I don't think a simple redirect will do the trick, because I need to specify the HTTP method. Is this the right way to do this, and if so, how do I tell Javascript about the HTTP method?
Thank you
Ok, I tried XMLHttpRequest, but couldn't get it to work. I ended up doing this, which is kind of hacktastic, but it works:
login = function(url) {
$.ajax({
url: url,
type: "GET"
}).done(function(){
window.location.href = url;
});
}
logout = function(url) {
$.ajax({
url: url,
type: "DELETE"
}).done(function(){
window.location.href = "/";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With