1

I am on chapter 11 of Michael Hartl's tutorial and unable to follow users. I click the follow button but nothing happens. I've went over that section over and over, copy and pasted the code. It still doesn't work.

Here is the code:

relationships_controller.rb

class RelationshipsController < ApplicationController
before_action :signed_in_user

def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
  format.html { redirect_to @user }
  format.js
end
end

def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
  format.html { redirect_to @user }
  format.js
end
end
end

create.js.erb

$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>")
$("#followers").html('<%= @user.followers.count %>')

destroy.js.erb

$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>")
$("#followers").html('<%= @user.followers.count %>')

_follow.html.erb

<%= form_for(current_user.relationships.build(followed_id: @user.id),
         remote: true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>

_unfollow.html.erb

<%= form_for(current_user.relationships.find_by(followed_id: @user.id),
         html: { method: :delete },
         remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>

_follow_form.html.erb

<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
JohnsWort
  • 118
  • 7

1 Answers1

0

Without seeing your logs, this is a game of speculation

--

However, there are a number of things to look into.

Create

Firstly, you need to make sure your create action can actually perform what you want. I notice there's a method .follow! included:

def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
end

Since you're using a bang operator, it generally means it will perform a function on the object itself (or at the time of calling). To this degree, you need to ensure you have the .follow! instance method in your User model

I would expect the following:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :relationships 

   def follow!(friend)
       self.relationships.create({follower_id = friend.id})
   end
end

--

Forms

As you're using form_for, you'll want to make sure you're calling the correct routes. I notice Hartl doesn't create an ActiveRecord Object before trying to populate the form_for - meaning this could cause an issue.

Since you're using a partial, I actually understand why this has been done. However, it could be the case that the form is not hitting the right action, or it's not sending the params you want

Although I'd love to elaborate on this, I cannot without seeing the logs

--

Ajax

Finally, you may wish to consider the response from Ajax.

I notice you're using the .js.erb method to perform an action when you've sent a remote request. Perhaps the functionality of these files is incorrect?

This could be a problem - if you don't see any other issue with your system (IE your routes and controller checks out), you'll want to ensure the .js.erb files are not messing up.

Again, this would be highlighted in your logs :)

Community
  • 1
  • 1
Richard Peck
  • 73,250
  • 8
  • 84
  • 139
  • 1
    I found that I used find_by instead of create for the follow! method in user.rb. Thanks for your help! – JohnsWort Jul 14 '14 at 10:49