1
0
Fork 0
find-us-lunch/app/controllers/friendships_controller.rb

35 lines
814 B
Ruby
Raw Permalink Normal View History

2014-10-15 13:32:35 -04:00
class FriendshipsController < ApplicationController
2014-10-15 13:50:58 -04:00
before_action :require_login
2014-10-15 13:32:35 -04:00
def index
2014-10-27 11:06:31 -04:00
@friendships = Friendship.includes(:friend).where(user_id: current_user.id)
2014-10-15 13:32:35 -04:00
end
def create
2014-10-15 16:40:46 -04:00
@friendship = current_user.friendships.build(friendship_params)
if @friendship.save
2014-10-27 12:43:24 -04:00
flash[:success] = "#{@friendship.friend.name} added as friend"
2014-10-27 12:25:36 -04:00
redirect_to friendships_path
2014-10-15 16:40:46 -04:00
else
2014-10-27 12:43:24 -04:00
flash[:error] = "Unable to add friend: #{@friendship.error_messages}"
2014-10-27 12:25:36 -04:00
redirect_to users_path
2014-10-15 13:32:35 -04:00
end
end
def destroy
2014-10-27 11:06:31 -04:00
@friendship = Friendship.find(params[:id])
2014-10-15 16:46:36 -04:00
@friendship.destroy
flash[:success] = 'Friend has been removed from your friends'
redirect_to friendships_path
2014-10-15 13:32:35 -04:00
end
private
2014-10-15 16:40:46 -04:00
def friendship_params
# why not namespaced
params.permit(:friend_id)
end
2014-10-15 13:32:35 -04:00
end