Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ def show
end
end

def add_friend
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
@user = User.find(params[:id])

unless Friendship.exists?(:user_id => @current_user.id, :friend_id => params[:id])
unless Friendship.exists?(:user_id => params[:id], :friend_id => @current_user.id)
unless @current_user.id == @user.id
friendship = Friendship.new
friendship.user_id = @current_user.id
friendship.friend_id = @user.id
friendship.save
end
end
end

end

def logout
cookies.delete(:auth_token)
redirect_to :root
Expand Down
7 changes: 7 additions & 0 deletions app/models/friendship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Friendship < ActiveRecord::Base
belongs_to :user
# 其模型并不存在,所以要给出 :class_name 及 :foreign_key 选项。
# 以让活动记录知道上哪里去找记录。
belongs_to :friend, :class_name => "User", :foreign_key => "user_id"

end
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class User < ActiveRecord::Base
has_many :chatrooms, :through => :room_mems
has_many :room_mems
has_many :friendships
# 创建 has_many :through 关联,在 friendships 内找寻 friends。
has_many :friends, :through => :friendships

has_secure_password

before_create { generate_token(:auth_token) }

def generate_token(column)
Expand Down
31 changes: 31 additions & 0 deletions app/views/users/add_friend.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h1>
用户
<%= @user.username %>
的个人主页哟
</h1>
<table>
<thead>
<tr>
<th class="name">头像</th>
<th>成员id</th>
<th>性别</th>
<th>手机号</th>
<th>状态</th>
<th>最近登录</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<tr>
<td class="pic"><%= image_tag(@user.avatar) %></td>
<td><%= @user.username %></td>
<td><%= @user.gender %></td>
<td><%= @user.phone %></td>
<td><%= @user.status %></td>
<td><%= @user.updated_at %></td>
<td><%= link_to '站内信', "#" %></td>
</tr>
</tbody>
</table>
<td><%= link_to '回到外婆桥', :root %></td>
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<td><%= @user.phone %></td>
<td><%= @user.status %></td>
<td><%= @user.updated_at %></td>
<td><%= link_to '加为好友', "#" %></td>
<td><%= button_to '加为好友', addfriend_path(@user.id), :method => :put %></td>
</tr>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
delete "logout" => "users#logout", :as => "logout"

get "users/:id" => "users#show", :as => "showuser"
put "users/:id" => "users#add_friend", :as => "addfriend"
get "chatrooms/:id/enter" => "room_mems#enter", :as => "enterroom"

root 'homepage#index'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20150604064251_create_friendships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateFriendships < ActiveRecord::Migration
def change
create_table :friendships do |t|
t.integer :user_id
t.integer :friend_id

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150603100813) do
ActiveRecord::Schema.define(version: 20150604064251) do

create_table "chatrooms", force: true do |t|
t.string "roomname"
Expand All @@ -27,6 +27,13 @@
t.text "description"
end

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

create_table "room_mems", force: true do |t|
t.integer "chatroom_id"
t.integer "user_id"
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/friendships.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
user_id: 1
friend_id: 1

two:
user_id: 1
friend_id: 1
7 changes: 7 additions & 0 deletions test/models/friendship_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class FriendshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end