last day (15 days later) » 

7:00 PM
0
A: Authenticating with Acts As Follower and Devise

Sachin PrasadThis should fix it (Update your if condition): <% if current_user.present? && current_user.following?(@celebrity) %> Regarding User login redirect, this should work(sorry I don't have time to test): <script> $(function() { $(".follow,.unfollow").bind("ajax:error", function(event, ...

 
That seemed to fix the the view code still need a way to redirect non-registered users to the sign up page if the try click the follow button. I tried using the before action authenticate_user!, only: [:follow, :unfollow] but it didn't do anything.
 
Since your request is an Ajax call, it can not redirect the user, you will have to manually check the response status of the ajax call and redirect the user to login if he is not logged in.
 
What would be the best way to do so? I'm not the greatest when it comes to Ajax. In addition to marking this answer as correct I can definitely award a bonus point to explain things further if its not too much to ask.
 
@CarlEdwards Please check my updated answer. I'm checking the response status of the AJAX call returned and if its unauthorized I'm redirecting that user to login page.
 
With that I get this error in my console: POST http://localhost:3000/celebrities/2/follow 401 (Unauthorized)
 
7:00 PM
Please try now ! I had xhr in caps.
 
Unfortunately I got the same error.
 
can you try this: alert(xhr.status); before if condition. What are you getting?
 
The alert message read undefined
Hey how's it going
question about the ajax function
should I put it in my original conditional script from my original question
its the one where I change the links according to the link's text
 
try this : alert(status);
 
what it's doing now is nesting a new link inside the follow/unfollow link
k one sec
that gave me "error"
 
7:04 PM
Where did you put it?
  $(".follow,.unfollow").bind("ajax:error", function(event, data, status, xhr) {
    alert(status);if (xhr.status == 401){
      window.location.replace("<%= new_user_session_path %>");
    }
  });
 
right now my code looks like this
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_celebrity_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_celebrity_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})
})

$(".follow,.unfollow").bind("ajax:error", function(event, data, status, xhr) {
alert(status);
The nested link looks like its coming from the first click function
trying to figure out why
 
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(event, data, status, xhr) {
alert(status);
alert('::::::'+xhr.status);
try this
 
With that I first get the message "error"
then another one that says "::::::undefined"
 
You should be getting a response code of 401.
 
Yeah getting that message in my console
those two were in the alert boxes
 
7:13 PM
try this:
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(data, status, xhr) {
alert(status);
alert('::::::'+xhr.status);
 
still get that 401 error in my console
but get 3 alerts
undefined
error
[object Object]
would it be easier to put the project on github for you?
 
That will take time as I'll have to configure the project.
Are you sure you got those values?
Can you reload?
 
unfortunately yes
 
As this value shouldn't be undefined : alert('::::::'+xhr.status);
:::::::: will atleast be printed
*on alert
 
with that particular alert this is printed
::::::undefined
 
7:20 PM
Please try this :
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
alert(status);
alert('::::::'+xhr.status);
 
Now we're getting somewhere
So the first alert says "error"
the second "::::::401"
then the third says "{"readyState":4,"responseText":"You need to sign in or sign up before continuing.","status":401,"statusText":"Unauthorized"}"
and I get redirected to the sign up page
 
Finally .. actually the way I was passing the parameters were not correct :
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
if (xhr.status == 401) {
 
The only thing now is that the value of the celebritie's followers isn't changing
scratch that
some extra code I needed to take out
one a lasting note for the unregistered users
how would I go about in redirecting the signed in users to that page?
 
I didn't get you ..
 
crap before I mention that
for some reason I'm getting this error
POST localhost:3000/celebrities/2/follow 500 (Internal Server Error)
now its a 500 error
okay I made a fix on my end but to you does this look applicable?
$(function() {
$('a').click(function() {
if ($(this).text() == "Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
if (xhr.status == 401 || xhr.status == 500 ) {
Notice this line: if (xhr.status == 401 || xhr.status == 500 )
i don't get the difference?
 
7:33 PM
One thing try this code:
$(function() {
$(document.body).on("click",".follow,.unfollow",(function() {
if ($(this).hasClass("Unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
})

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
Also there is some error in your controller I guess check the logs
That not a gud I idea to redirect a user to login page on internal server error.
 
this particular code didn't work as opposed to the first
 
Please check now:
$(function() {
$(document.body).on("click",".follow,.unfollow",(function() {
if ($(this).hasClass("unfollow") {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
});

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
 
one sec
I'm trying to see why its complaining
in the first conditional I needed to add en extra ")" at the end
but now its saying
Uncaught SyntaxError: Unexpected token ;
 
8:02 PM
try this
$(function() {
$(document.body).on("click", ".follow,.unfollow", (function() {
if ($(this).hasClass("unfollow")) {
$(this).html("<%= escape_javascript( link_to 'Follow', follow_artist_path, method: :post, remote: true) %>").addClass('follow').removeClass('unfollow');
} else {
$(this).html("<%= escape_javascript( link_to 'Unfollow', unfollow_artist_path, method: :post, remote: true) %>").addClass('unfollow').removeClass('follow');
}
});

$(".follow,.unfollow").bind("ajax:error", function(data, xhr, status, error) {
 
gimme a minute
still has an unexpected token
its pointing here:
$(this).html("<a data-method=\"post\" data-remote=\"true\" href=\"/artists/2/unfollow\" rel=\"nofollow\">Unfollow<\/a>").addClass('unfollow').removeClass('follow');
}
}); // This semicolon
 

last day (15 days later) »