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?