0

I'm trying to create a friendship between 2 users and I'm using the tutorial

I have this as my friendships_controller.rb,

def create
  @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
  if @friendship.save
    flash[:notice] = "Added friend."
    redirect_to root_url
  else
    flash[:error] = "Unable to add friend."
    redirect_to root_url
  end
end

And in Angular I have this function,

$scope.addFriend = function (user) {
  $scope.user = user
  console.log ($scope.user)

  createFriend.create({
    friend_id:   $scope.user.id
  })
}

And this is the create methode in my Angular service,

app.factory('createFriend', ['$http', function($http) {
  return {
    create: function() {
      return $http.post('/friendships.json');
    }
  };
}])

When I add a friend I get this in my rails console,

Started POST "/friendships.json" for 127.0.0.1 at 2015-12-29 12:51:59 +0100
Processing by FriendshipsController#create as JSON
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (9.9ms)  INSERT INTO "friendships" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["user_id", 1], ["created_at", "2015-12-29 11:51:59.550777"], ["updated_at", "2015-12-29 11:51:59.550777"]]
   (17.6ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 39ms (ActiveRecord: 27.7ms)

As you can see from the console output the friend_id parameter is missing.

It's added in the database thought,

create_table "friendships", force: :cascade do |t|
  t.integer  "user_id"
  t.integer  "friend_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

When I run Friendship.all in my rails console I get,

<Friendship id: 1, user_id: 1, friend_id: nil, created_at: "2015-12-29 11:36:46", updated_at: "2015-12-29 11:36:46">

Any ideas why the friend_id parameter isn't being used?

2
  • can you please include the createFriend.create method code? or are you sure it's passing the params correctly? Commented Dec 29, 2015 at 12:18
  • 1
    @basia I've added the create method from my Angular service. Commented Dec 29, 2015 at 12:25

1 Answer 1

1

You need to pass arguments from createFriend.create to $http.post. If you look at the documentation, $http.post takes url as first argument and request content as second. Right now, {friend_id: $scope.user.id} is simply being ignored. Something like (not tested) code below should do the trick.

app.factory('createFriend', ['$http', function($http) {
  return {
    create: function(data) {
      return $http.post('/friendships.json', data);
    }
  };
}])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I missed that part. Small note, instead of data I can use user. Might make it a bit more readable. Thanks for the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.