Can only add a friend one time
This commit is contained in:
parent
461d3c6b7b
commit
1919d618aa
5 changed files with 17 additions and 6 deletions
|
@ -10,10 +10,10 @@ class FriendshipsController < ApplicationController
|
||||||
|
|
||||||
if @friendship.save
|
if @friendship.save
|
||||||
flash[:notice] = 'Added friend.'
|
flash[:notice] = 'Added friend.'
|
||||||
redirect_to root_url
|
redirect_to friendships_path
|
||||||
else
|
else
|
||||||
flash[:notice] = 'Unable to add friend.'
|
flash[:notice] = 'Unable to add friend.'
|
||||||
redirect_to root_url
|
redirect_to users_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,5 +4,12 @@ class UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@user = User.find(params[:id])
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def user_params
|
||||||
|
params.permit(:id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
class Friendship < ActiveRecord::Base
|
class Friendship < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :friend, class_name: 'User'
|
belongs_to :friend, class_name: 'User'
|
||||||
|
|
||||||
|
validates_uniqueness_of :friend_id, :scope => :user_id
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<h2>Listing friendships</h2>
|
<h2>Friends List</h2>
|
||||||
|
|
||||||
<table class="table table-striped table-hover">
|
<table class="table table-striped table-hover">
|
||||||
<thead>
|
<thead>
|
||||||
|
@ -13,7 +13,7 @@
|
||||||
<tbody>
|
<tbody>
|
||||||
<% @friendships.each do |friendship| %>
|
<% @friendships.each do |friendship| %>
|
||||||
<tr>
|
<tr>
|
||||||
<td><%= friendship.friend.name %></td>
|
<td><%= link_to friendship.friend.name, friendship.friend %></td>
|
||||||
<td><%= friendship.created_at %></td>
|
<td><%= friendship.created_at %></td>
|
||||||
<td><%= friendship.updated_at %></td>
|
<td><%= friendship.updated_at %></td>
|
||||||
<td>[ <%= link_to 'Destroy', friendship, method: :delete,
|
<td>[ <%= link_to 'Destroy', friendship, method: :delete,
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
<h1>User#show</h1>
|
<h2><%= @user.name %></h2>
|
||||||
<p>Find me in app/views/user/show.html.erb</p>
|
Friends: <%= @user.friends.count %><br/>
|
||||||
|
Created: <%= @user.created_at %><br/>
|
||||||
|
Updarted: <%= @user.updated_at %><br/>
|
||||||
|
|
Loading…
Reference in a new issue