Unfollowing Users

A few readers have also asked me to add an Unfollow button for the sample social network in Rails Crash Course. Here’s how I made that happen:

I created a new branch called unfollow-user and added the Unfollow button.

Here are the steps I followed:

  1. Update config/routes.rb to include a route for unfollow_user. Since this removes a row from the database, I used the DELETE HTTP verb.
  2. Add an unfollow! method to app/models/user.rb, similar to the follow! method.
  3. Add an unfollow action to app/controllers/users_controller.rb, similar to the follow action.
  4. Add a Unfollow button to the view at app/views/users/show.html.erb. I also added some logic to show the correct button.

The changes are in this commit: 29fa67a

Hope this helps!

3 thoughts on “Unfollowing Users”

  1. Hey there. I’m building the social app and have run into an issue at the very start.

    I’m getting “NoMethodError: undefined method `name’ for nil:NilClass” when trying “alice.follow! bob”. The “alice.following? bob” method works though.

    I’ve rebuilt the app 3 times, copied the user.rb code from your github and cannot find where my mistake is. Any pointers?

    Enjoying your book. Chapter 7 was very illuminating!


    (Ruby 2.2.1 if that matters)

    Full error:

    User Exists (0.1ms) SELECT 1 AS one FROM “users” INNER JOIN “subscriptions” ON “users”.”id” = “subscriptions”.”leader_id” WHERE “subscriptions”.”follower_id” = ? AND “users”.”id” = 11 LIMIT 1 [[“follower_id”, 10]]
    (0.1ms) begin transaction
    SQL (0.5ms) INSERT INTO “subscriptions” (“created_at”, “follower_id”, “leader_id”, “updated_at”) VALUES (?, ?, ?, ?) [[“created_at”, Wed, 17 Jun 2015 04:23:21 UTC +00:00], [“follower_id”, 10], [“leader_id”, 11], [“updated_at”, Wed, 17 Jun 2015 04:23:21 UTC +00:00]]
    (8.5ms) rollback transaction
    NoMethodError: undefined method `name’ for nil:NilClass

  2. Hey there. Disregard my comment. I installed the github project and it’s working now. I have no idea what the issue was, but at least I can move on.


  3. Hi, I’m working through your Rails Crash Course book, so far I have had minimal issues and find it to be a great resource. I have, however, recently ran into an issue I cannot resolve. I cannot get the final curl command in Chapter 14 to work (final curl command before the Summary section). I have checked and double checked everything and still cannot update my site through the API. Heres what my server outputs:

    Started POST “/api/text_posts” for ::1 at 2015-10-05 ….
    ActiveRecord::SchemaMigration Load (0.0ms) SELECT “schema_migrations”.*FROM “schema_migrations”
    Error occured while parsing request parameters

    ActionDispatch::ParamsParser::ParseError (795: unexpected token at ”{text_post:{title:Test,body:Hello}}”):

    any ideas?

Comments are closed.