Has anyone setup Acts_as_Follower Gem Successfully?

If you happen to have a complete solution for setting up
Acts_As_Follower Gem. Please, post it for your fellow devs.

Users_Controller.rb [content]
Routes.rb [content]
_follow.html.erb [button]
JS for sending AJAX request and changing the state of the button from
follow to unfollow.

Lastly, adding a block button and unblock button.

On Tuesday, 12 January 2016 00:12:04 UTC-5, Ruby-Forum.com User wrote:

follow to unfollow.

A quick word of advice: try looking for things before asking here. For
instance, the FIRST HIT on Google for the query “acts_as_follower ajax”
(without quotes) is this:

The comments on that Gist appear to imply that not all of it will work
on
Rails 4. I didn’t see anything that jumped out as being deprecated /
removed between 3 and 4 but I haven’t tried it out. I’d recommend you
try
that code and report back if there are problems you can’t sort out.

Lastly, adding a block button and unblock button.

Again, you’ll get much better assistance here if you try things yourself
first. If you want requirements turned directly into code, email me
off-list and let me know where to send the INVOICE. :slight_smile:

–Matt J.

Matt J. wrote in post #1180578:

On Tuesday, 12 January 2016 00:12:04 UTC-5, Ruby-Forum.com User wrote:

follow to unfollow.

A quick word of advice: try looking for things before asking here. For
instance, the FIRST HIT on Google for the query “acts_as_follower ajax”
(without quotes) is this:

sample of how to use acts_as_follower with rails3 and ajax · GitHub

The comments on that Gist appear to imply that not all of it will work
on
Rails 4. I didn’t see anything that jumped out as being deprecated /
removed between 3 and 4 but I haven’t tried it out. I’d recommend you
try
that code and report back if there are problems you can’t sort out.

–Matt J.

I will report back once I’ve implemented the solution for myself.

Matt J. wrote in post #1180578:

On Tuesday, 12 January 2016 00:12:04 UTC-5, Ruby-Forum.com User wrote:

follow to unfollow.

I’m returning for your advice and help. I’ve implemented a
Follows_Controller.Rb Below

class FollowsController < ApplicationController
def create
@user = User.find(params[:user_id])
current_user.follow(@user) unless current_user.blocked?(@user)
end

def destroy
@user = User.find(params[:user_id])
current_user.stop_following(@user)
end
end

The JavaScript create and destroy files for the AJAX buttons.

create.js.erb
$(‘#follow_user’).html(‘<%= escape_javascript(render :partial =>
“follows/follow_user”, :locals => {:user => @user}) %>’);

destroy.js.erb
$(‘#follow_user’).html(‘<%= escape_javascript(render :partial =>
“follows/follow_user”, :locals => {:user => @user}) %>’);

The follow user partial file.
<% if user_signed_in? %>
<% unless @user == current_user %>
<% if current_user.following?(@user) %>
<%= button_to(‘Stop Following’, user_follow_path(user,
current_user.get_follow(@user).id), :method => :delete, :remote => true,
class: ‘btn btn-danger-outline’) %>
<% else %>
<%= button_to(‘Follow’, user_follows_path(@user), :remote =>
true, class: ‘btn btn-success-outline’) %>
<% end %>
<% end %>
<% end %>

Lastly, the routes.
resources :follows, :only => [:create, :destroy]

When I render the partial inside of the _posts.html.erb file, it gives
me an error. First it was ‘user’ method cannot be found, and then I used
the @user instead. Afterwards it started to say undefined method id for
nil:NilClass. Why am I unable to use the follow/unfollow buttons?

I want the buttons to show, but only redirect users that are not logged
in.

On Thursday, 21 January 2016 22:21:37 UTC-6, Ruby-Forum.com User wrote:

class FollowsController < ApplicationController

<% end %>

the @user instead.
The behavior you’ve described doesn’t match the code. The :locals => { :user => @user } part of those render calls should have made user
available in the partial. If you’re switching to using the instance
variable, make sure to remove all the references to the local variable
(there’s still one in the call to user_follow_path).

Afterwards it started to say undefined method id for

nil:NilClass. Why am I unable to use the follow/unfollow buttons?

That message isn’t particularly helpful without a stack trace; something
is
calling id on an object that’s nil. A stack trace would be helpful for
finding the cause.

Regarding your followup message (“I want the buttons to show, but only
redirect users that are not logged in.”), that may require some
rethinking
of your approach. Doing a redirect from the FollowsController#create
method will not do what you want (the browser will follow the redirect
and
attempt to parse the result as Javascript).

–Matt J.

Matt J. wrote in post #1180799:

On Thursday, 21 January 2016 22:21:37 UTC-6, Ruby-Forum.com User wrote:

class FollowsController < ApplicationController

<% end %>

the @user instead.
The behavior you’ve described doesn’t match the code. The :locals => { :user => @user } part of those render calls should have made user
available in the partial. If you’re switching to using the instance
variable, make sure to remove all the references to the local variable
(there’s still one in the call to user_follow_path).

Can you help me re-write the solution?

<% if user_signed_in? %>
<% unless @user == current_user %>
<% if current_user.following?(@user) %>
<%= button_to(‘Stop Following’,
user_follow_path(current_user.get_follow(@user)), :method => :delete,
:remote => true, class: ‘btn btn-danger-outline’) %>
<% else %>
<%= button_to(‘Follow’, user_follows_path(@user), :remote =>
true, class: ‘btn btn-success-outline’) %>
<% end %>
<% end %>
<% end %>

create.js.erb
$(‘#follow_user’).html(‘<%= escape_javascript(render :partial =>
“follows/follow_user”) %>’);

destroy.js.erb
$(‘#follow_user’).html(‘<%= escape_javascript(render :partial =>
“follows/follow_user”) %>’);


I’m getting this error now

ActionView::Template::Error (undefined method `id’ for nil:NilClass):
1: <% if user_signed_in? %>
2: <% unless @user == current_user %>
3: <% if current_user.following?(@user) %>
4: <%= button_to(‘Stop Following’,
user_follow_path(current_user.get_follow(@user)), :method => :delete,
:remote => true, class: ‘btn btn-danger-outline’) %>
5: <% else %>
6: <%= button_to(‘Follow’, user_follows_path(@user),
:remote => true, class: ‘btn btn-success-outline’) %>


Can you try to come up with a better approach, and then show me how I
should solve this problem? I know that it’s checking for the user id,
but the user may not be loaded yet.

Matt J. wrote in post #1180799:

They don’t have an updated wiki for the gem on Rails 4. Maybe, you and I
can put one together to solve this problem for once and for all.

I got it working, but now, I just have to add AJAX.

<% if user_signed_in? %>
<% if current_user.following?(@user) %>
<%= link_to “Unfollow”, unfollow_user_path(@user), class:
‘btn btn-danger’ %>
<% else %>
<%= link_to “Follow”, follow_user_path(@user), class: ‘btn
btn-success’ %>
<% end %>
<% end %>