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.
JS for sending AJAX request and changing the state of the button from
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.
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.
.....
--Matt Jones
I will report back once I've implemented the solution for myself.
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?
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.
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).
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 %>