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
2 changes: 1 addition & 1 deletion app/controllers/chatrooms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ def set_chatroom
end
# Never trust parameters from the scary internet, only allow the white list through.
def chatroom_params
params.require(:chatroom).permit(:roomname, :roomcover, :privacy, :popularity, :memnum, :roomno, :key, :creatorid, :description)
params.require(:chatroom).permit(:roomname, :roomcover, :privacy, :popularity, :memnum, :roomno, :key, :user_id, :description)
end
end
15 changes: 15 additions & 0 deletions app/controllers/room_mems_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ class RoomMemsController < ApplicationController
def show
@room_mems = RoomMem.where(:chatroom_id => params[:id])
end

def enter
@room_mem = RoomMem.new
@user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
@room_mem.chatroom_id = params[:id]

if RoomMem.exists?(:user_id => @user.id, :chatroom_id => params[:id])
else
@room_mem.user_id = @user.id
@chatroom = @room_mem.chatroom
@chatroom.memnum = @chatroom.memnum+1
@chatroom.save
@room_mem.save
end
end
end
8 changes: 8 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ def login

end

def show
if User.exists?(params[:id])
@user = User.find(params[:id])
else
redirect_to :root
end
end

def logout
cookies.delete(:auth_token)
redirect_to :root
Expand Down
2 changes: 2 additions & 0 deletions app/models/chatroom.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Chatroom < ActiveRecord::Base
has_many :users, :through => :room_mems
has_many :room_mems
has_many :tags, :through => :room_tags
has_many :room_mems
before_create { generate_room_no(:roomno) }

def generate_room_no(column)
Expand Down
4 changes: 4 additions & 0 deletions app/models/room_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class RoomTag < ActiveRecord::Base
belongs_to :tag
belongs_to :chatroom
end
2 changes: 0 additions & 2 deletions app/models/roomtag.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Tag < ActiveRecord::Base
has_many :chatrooms, :through => :room_tags
has_many :room_tags
end
10 changes: 5 additions & 5 deletions app/views/chatrooms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
</div>
<% end %>
<div class="field">
<%= f.label :roomname %><br>
<%= f.label "名称" %><br>
<%= f.text_field :roomname %>
</div>
<div class="field">
<%= f.label :roomcover %><br>
<%= f.label "封面url" %><br>
<%= f.text_field :roomcover %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.label "描述" %><br>
<%= f.text_area :description, rows: 6 %>
</div>
<div class="field">
<%= f.label :privacy %><br>
<%= f.label "是否公开" %><br>
<%= f.text_field :privacy %>
</div>
<div class="field">
<%= f.label :key %><br>
<%= f.label "密码" %><br>
<%= f.number_field :key %>
</div>
<div class="actions">
Expand Down
4 changes: 2 additions & 2 deletions app/views/chatrooms/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>New chatroom</h1>
<h1>新建聊天室</h1>

<%= render 'form' %>

<%= link_to 'Back', :root %>
<%= link_to '返回主页', :root %>
2 changes: 1 addition & 1 deletion app/views/chatrooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
</p>
</div>
<%= link_to '查看成员列表', room_mem_path %> |
<%= link_to '编辑', edit_chatroom_path(@chatroom) %> |
<%= link_to '进入聊天室', enterroom_path(@chatroom.id) %> |
<%= link_to '返回主页', :root %>
5 changes: 5 additions & 0 deletions app/views/room_mems/enter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>
聊天中
</h1>

<td><%= link_to '回到外婆桥', :back %></td>
2 changes: 1 addition & 1 deletion app/views/room_mems/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td><%= item.user.phone %></td>
<td><%= item.user.status %></td>
<td><%= item.user.updated_at %></td>
<td><%= link_to 'Show', "www.baidu.com" %></td>
<td><%= link_to '用户主页', showuser_path(item.user.id) %></td>
</tr>
<% end %>
</tbody>
Expand Down
31 changes: 31 additions & 0 deletions app/views/users/show.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 '回到外婆桥', :back %></td>
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Rails.application.routes.draw do

resources :chatrooms
resources :users, only: [:create]
resources :room_mems

get "signup" => "users#signup", :as => "signup"
get "login" => "users#login", :as => "login"
post "create_login_session" => "users#create_login_session"
delete "logout" => "users#logout", :as => "logout"
resources :users, only: [:create]

resources :room_mems

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

root 'homepage#index'
# The priority is based upon order of creation: first created -> highest priority.
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20150603095905_drop_roomtags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DropRoomtags < ActiveRecord::Migration
def up
drop_table :roomtags
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
10 changes: 10 additions & 0 deletions db/migrate/20150603100813_create_room_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateRoomTags < ActiveRecord::Migration
def change
create_table :room_tags do |t|
t.integer :chatroom_id
t.integer :tag_id

t.timestamps
end
end
end
8 changes: 4 additions & 4 deletions 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: 20150603052523) do
ActiveRecord::Schema.define(version: 20150603100813) do

create_table "chatrooms", force: true do |t|
t.string "roomname"
Expand All @@ -34,9 +34,9 @@
t.datetime "updated_at"
end

create_table "roomtags", force: true do |t|
t.integer "chatroomid"
t.integer "tagid"
create_table "room_tags", force: true do |t|
t.integer "chatroom_id"
t.integer "tag_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/chatrooms_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ChatroomsControllerTest < ActionController::TestCase
post :create, chatroom: { user_id: @chatroom.user_id, key: @chatroom.key, memnum: @chatroom.memnum, popularity: @chatroom.popularity, privacy: @chatroom.privacy, roomcover: @chatroom.roomcover, roomname: @chatroom.roomname, roomno: @chatroom.roomno }
end

assert_redirected_to chatroom_path(assigns(:chatroom))
assert_redirected_to :root
end

test "should show chatroom" do
Expand All @@ -36,7 +36,7 @@ class ChatroomsControllerTest < ActionController::TestCase

test "should update chatroom" do
patch :update, id: @chatroom, chatroom: { user_id: @chatroom.user_id, key: @chatroom.key, memnum: @chatroom.memnum, popularity: @chatroom.popularity, privacy: @chatroom.privacy, roomcover: @chatroom.roomcover, roomname: @chatroom.roomname, roomno: @chatroom.roomno }
assert_redirected_to chatroom_path(assigns(:chatroom))
assert_redirected_to :root
end

test "should destroy chatroom" do
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/roomtags.yml → test/fixtures/room_tags.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
chatroomid: 1
tagid: 1
chatroom_id: 1
tag_id: 1

two:
chatroomid: 1
tagid: 1
chatroom_id: 1
tag_id: 1
4 changes: 2 additions & 2 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ one:
email: MyString
nickname: MyString
gender: false
password: MyString
password_digest: MyString
description: MyString
avatar: MyString
status: false
Expand All @@ -17,7 +17,7 @@ two:
email: MyString
nickname: MyString
gender: false
password: MyString
password_digest: MyString
description: MyString
avatar: MyString
status: false
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class RoomtagTest < ActiveSupport::TestCase
class RoomTagTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
Expand Down