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?
createFriend.createmethod code? or are you sure it's passing the params correctly?