From 1919d618aaf95d47d4ab7f386f49d0a207bd7ae6 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 27 Oct 2014 12:25:36 -0400 Subject: [PATCH] Can only add a friend one time --- app/controllers/friendships_controller.rb | 4 ++-- app/controllers/users_controller.rb | 7 +++++++ app/models/friendship.rb | 2 ++ app/views/friendships/index.html.erb | 4 ++-- app/views/users/show.html.erb | 6 ++++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/controllers/friendships_controller.rb b/app/controllers/friendships_controller.rb index 7aa233e..71371b3 100644 --- a/app/controllers/friendships_controller.rb +++ b/app/controllers/friendships_controller.rb @@ -10,10 +10,10 @@ class FriendshipsController < ApplicationController if @friendship.save flash[:notice] = 'Added friend.' - redirect_to root_url + redirect_to friendships_path else flash[:notice] = 'Unable to add friend.' - redirect_to root_url + redirect_to users_path end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index bbab75d..2eae0c1 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,5 +4,12 @@ class UsersController < ApplicationController end def show + @user = User.find(params[:id]) + end + + private + + def user_params + params.permit(:id) end end diff --git a/app/models/friendship.rb b/app/models/friendship.rb index 7d81232..bc7f6aa 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -1,4 +1,6 @@ class Friendship < ActiveRecord::Base belongs_to :user belongs_to :friend, class_name: 'User' + + validates_uniqueness_of :friend_id, :scope => :user_id end diff --git a/app/views/friendships/index.html.erb b/app/views/friendships/index.html.erb index de0c92f..c4db4ac 100644 --- a/app/views/friendships/index.html.erb +++ b/app/views/friendships/index.html.erb @@ -1,4 +1,4 @@ -

Listing friendships

+

Friends List

@@ -13,7 +13,7 @@ <% @friendships.each do |friendship| %> - +
<%= friendship.friend.name %><%= link_to friendship.friend.name, friendship.friend %> <%= friendship.created_at %> <%= friendship.updated_at %> [ <%= link_to 'Destroy', friendship, method: :delete, diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 301bd6e..61ddb30 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,2 +1,4 @@ -

User#show

-

Find me in app/views/user/show.html.erb

+

<%= @user.name %>

+Friends: <%= @user.friends.count %>
+Created: <%= @user.created_at %>
+Updarted: <%= @user.updated_at %>