Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

实现多个聊天室功能~~~ #11

Merged
merged 2 commits into from

2 participants

@songjiayang

No description provided.

@songjiayang songjiayang merged commit 3c40f5c into fireweb:master
@lidaobing

should use

@room = Room.find_by_id(params[:id])
head :not_found and return if @room.nil?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 7, 2013
  1. @songjiayang

    合并分支

    songjiayang authored
  2. @songjiayang
This page is out of date. Refresh to see the latest.
View
1  Gemfile
@@ -17,6 +17,7 @@ end
gem "twitter-bootstrap-rails"
gem 'jquery-rails'
gem 'gravtastic'
+gem 'will_paginate'
group :development do
gem "better_errors"
end
View
155 Gemfile.lock
@@ -1,155 +0,0 @@
-GEM
- remote: http://ruby.taobao.org/
- specs:
- actionmailer (3.2.13)
- actionpack (= 3.2.13)
- mail (~> 2.5.3)
- actionpack (3.2.13)
- activemodel (= 3.2.13)
- activesupport (= 3.2.13)
- builder (~> 3.0.0)
- erubis (~> 2.7.0)
- journey (~> 1.0.4)
- rack (~> 1.4.5)
- rack-cache (~> 1.2)
- rack-test (~> 0.6.1)
- sprockets (~> 2.2.1)
- activemodel (3.2.13)
- activesupport (= 3.2.13)
- builder (~> 3.0.0)
- activerecord (3.2.13)
- activemodel (= 3.2.13)
- activesupport (= 3.2.13)
- arel (~> 3.0.2)
- tzinfo (~> 0.3.29)
- activeresource (3.2.13)
- activemodel (= 3.2.13)
- activesupport (= 3.2.13)
- activesupport (3.2.13)
- i18n (= 0.6.1)
- multi_json (~> 1.0)
- addressable (2.3.3)
- arel (3.0.2)
- better_errors (0.7.2)
- coderay (>= 1.0.0)
- erubis (>= 2.6.6)
- builder (3.0.4)
- coderay (1.0.9)
- coffee-rails (3.2.2)
- coffee-script (>= 2.2.0)
- railties (~> 3.2.0)
- coffee-script (2.2.0)
- coffee-script-source
- execjs
- coffee-script-source (1.6.2)
- cookiejar (0.3.0)
- daemons (1.1.9)
- em-http-request (1.0.3)
- addressable (>= 2.2.3)
- cookiejar
- em-socksify
- eventmachine (>= 1.0.0.beta.4)
- http_parser.rb (>= 0.5.3)
- em-socksify (0.2.1)
- eventmachine (>= 1.0.0.beta.4)
- erubis (2.7.0)
- eventmachine (1.0.3)
- execjs (1.4.0)
- multi_json (~> 1.0)
- faye (0.8.9)
- cookiejar (>= 0.3.0)
- em-http-request (>= 0.3.0)
- eventmachine (>= 0.12.0)
- faye-websocket (>= 0.4.0)
- rack (>= 1.0.0)
- yajl-ruby (>= 1.0.0)
- faye-websocket (0.4.7)
- eventmachine (>= 0.12.0)
- gravtastic (3.2.6)
- hike (1.2.1)
- http_parser.rb (0.5.3)
- i18n (0.6.1)
- journey (1.0.4)
- jquery-rails (2.2.1)
- railties (>= 3.0, < 5.0)
- thor (>= 0.14, < 2.0)
- json (1.7.7)
- mail (2.5.3)
- i18n (>= 0.4.0)
- mime-types (~> 1.16)
- treetop (~> 1.4.8)
- mime-types (1.22)
- multi_json (1.7.2)
- mysql2 (0.3.11)
- pg (0.15.0)
- polyglot (0.3.3)
- rack (1.4.5)
- rack-cache (1.2)
- rack (>= 0.4)
- rack-ssl (1.3.3)
- rack
- rack-test (0.6.2)
- rack (>= 1.0)
- rails (3.2.13)
- actionmailer (= 3.2.13)
- actionpack (= 3.2.13)
- activerecord (= 3.2.13)
- activeresource (= 3.2.13)
- activesupport (= 3.2.13)
- bundler (~> 1.0)
- railties (= 3.2.13)
- railties (3.2.13)
- actionpack (= 3.2.13)
- activesupport (= 3.2.13)
- rack-ssl (~> 1.3.2)
- rake (>= 0.8.7)
- rdoc (~> 3.4)
- thor (>= 0.14.6, < 2.0)
- rake (10.0.4)
- rdoc (3.12.2)
- json (~> 1.4)
- sass (3.2.7)
- sass-rails (3.2.6)
- railties (~> 3.2.0)
- sass (>= 3.1.10)
- tilt (~> 1.3)
- sprockets (2.2.2)
- hike (~> 1.2)
- multi_json (~> 1.0)
- rack (~> 1.0)
- tilt (~> 1.1, != 1.3.0)
- thin (1.5.1)
- daemons (>= 1.0.9)
- eventmachine (>= 0.12.6)
- rack (>= 1.0.0)
- thor (0.18.1)
- tilt (1.3.6)
- treetop (1.4.12)
- polyglot
- polyglot (>= 0.3.1)
- twitter-bootstrap-rails (2.2.6)
- actionpack (>= 3.1)
- execjs
- railties (>= 3.1)
- tzinfo (0.3.37)
- uglifier (1.3.0)
- execjs (>= 0.3.0)
- multi_json (~> 1.0, >= 1.0.2)
- yajl-ruby (1.1.0)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- better_errors
- coffee-rails (~> 3.2.1)
- faye
- gravtastic
- jquery-rails
- mysql2
- pg
- rails (= 3.2.13)
- sass-rails (~> 3.2.3)
- thin
- twitter-bootstrap-rails
- uglifier (>= 1.0.3)
View
2  README.md
@@ -20,4 +20,4 @@ Chater 是一个在线聊天室,一个实时web应用,一个基础实践....
##参考文档
* [How to Use Faye as a Real-Time Push Server in Rails](http://net.tutsplus.com/tutorials/ruby/how-to-use-faye-as-a-real-time-push-server-in-rails/)
-##Preview
+##Preview
View
3  app/assets/javascripts/application.js
@@ -1,4 +1,3 @@
-
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
@@ -19,4 +18,4 @@ function current_time () {
if (min<0) {min="0"+min};
if (sec<0) {sec= "0"+sec};
return year+"-"+mon+"-"+date+" "+hour+":"+min+":"+sec
-}
+}
View
81 app/assets/stylesheets/room.css.erb
@@ -29,7 +29,7 @@
width: 100%;
border-radius:10px;
overflow:auto;
- background: rgba(20%, 50%, 80%, 0.5);
+ background: rgba(20%, 50%, 80%, 0.8);
}
@@ -94,7 +94,80 @@
margin-top: 0px;
}
-#user_name{
- color: #08C;
- font-weight:bold;
+.welcome-message span{
+ color: #08C;
+ font-weight:bold;
+}
+
+.room_list{
+ width: 20%;
+ display: block;
+ padding: 4px;
+ height:180px;
+ position:relative;
+ line-height: 1;
+ background: rgba(255, 255, 255, 0.7);
+ overflow:hidden;
+ text-overflow:ellipsis;
+ border: 1px solid #DDD;
+ -webkit-border-radius: 4px;
+ -moz-border-radius: 4px;
+ border-radius: 4px;
+ -webkit-box-shadow: 0 3px 2px rgba(0, 0, 0,0.8);
+ -moz-box-shadow: 0 3px 2px rgba(0, 0, 0, 0.8);
+ box-shadow: 0 3px 2px rgba(0, 0, 0, 0.8);
+}
+.thumbnails li{
+ margin-left:2%;
+}
+
+.room_list h5{
+ font-size:16px;
+ border-bottom:1px solid #ccc;
+ padding:5px;
+}
+
+.room_list a:hover {
+ cursor: pointer;
+ color:#ff0033;
+ text-decoration:none;
+}
+
+.room_list .like{
+ float:right;
+ margin-right:2px;
+ font-size:18px;
+ color:#646464;
+}
+
+.room_list .left{
+ float:left;
+ margin-left:4px;
+ font-size:14px;
+ color:#646464;
+ font-weight: bold;
+}
+
+.room_list .right{
+ position:absolute;
+ margin-left: 83%;
+ color:#646464;
+ font-size:14px;
+ font-weight: bold;
+}
+
+
+.room_list .desc{
+ padding: 4px;
+ font-size: 14px;
+ height: 97px;
+}
+
+.thumbnails{
+ padding-left: 8%;
+}
+
+
+.pagination{
+ text-align: center;
}
View
11 app/controllers/rooms_controller.rb
@@ -0,0 +1,11 @@
+class RoomsController < ApplicationController
+
+ def index
+ redirect_to login_path unless session[:username]
+ @rooms = Room.order("id desc").paginate(:page => params[:page], :per_page => 12)
+ end
+
+ def show
+ @room = Room.find(params[:id])
+ end
+end
View
2  app/controllers/sessions_controller.rb
@@ -5,6 +5,6 @@ def new
def create
session[:username] = params[:username]
- redirect_to chat_path
+ redirect_to chat_rooms_path
end
end
View
4 app/models/room.rb
@@ -0,0 +1,4 @@
+class Room < ActiveRecord::Base
+ attr_accessible :description, :title
+ validates :description, :title, :presence => true
+end
View
26 app/views/chats/room.html.erb
@@ -1,4 +1,3 @@
-<%= javascript_include_tag "room"%>
<div class="container-fluid">
<div class="row-fluid">
<div class="span9">
@@ -39,24 +38,18 @@
</div>
<script>
- $(function() {
- $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
- $('.messages').css('height',window.screen.height-285);
- $('.subject').css('max-height',window.screen.height-550);
- var client = new Faye.Client('http://localhost:9292/faye');
- $('#submit').click(function(){
- client.publish('/messages/new', {
+ $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
+ $('.messages').css('height',window.screen.height-285);
+ $('.subject').css('max-height',window.screen.height-550);
+ $('#submit').click(function(){
+ faye.publish('/messages/new', {
username: '<%= session[:username] %>',
msg: $('#message').val()
- });
- $('#message').val('');
- return false;
- });
+ });
+ $('#message').val('');
+ return false;
});
-
- $(function(){
- var client = new Faye.Client('http://localhost:9292/faye');
- client.subscribe("/messages/new",function(data){
+ faye.subscribe("/messages/new",function(data){
var a_new_message = ('<li><div class="user-avatar"> <img src="'+'http://l.ruby-china.org/user/large_avatar/827.jpg"');
a_new_message +='/></div><div class="info"><span class="username">';
a_new_message +=data.username;
@@ -64,7 +57,6 @@
a_new_message +=('<span >'+ current_time()+'</span>');
a_new_message +=('<p>'+data.msg+'</p></div></li>');
$('.messages').append(a_new_message);
- });
});
</script>
View
5 app/views/layouts/application.html.erb
@@ -5,10 +5,11 @@
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" ,"http://localhost:9292/faye.js" %>
<%= csrf_meta_tags %>
+ <script type="text/javascript">
+ var faye = new Faye.Client('http://localhost:9292/faye');
+ </script>
</head>
<body>
-
<%= yield %>
-
</body>
</html>
View
66 app/views/rooms/_topbar.html.erb
@@ -0,0 +1,66 @@
+<div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container-fluid">
+ <a class="brand" href=/>
+ Chater
+ </a>
+ <ul class="nav">
+ <li class="active">
+ <a href="/">Start</a>
+ </li>
+ <li>
+ <a href="/rooms/new" data-remote="true">New Room</a>
+ </li>
+
+ <li class="divider-vertical"></li>
+ <li class="dropdown">
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#fat">
+ My Favs
+ <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu fav" id="fav_rooms">
+
+ <li><a href="/rooms/17">Feedback</a></li>
+
+ </ul>
+ </li>
+
+ <li class="divider-vertical"></li>
+ <li class="dropdown">
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#fat">
+ My Rooms
+ <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu fav" id="fav_rooms">
+
+ <li><a href="/rooms/41">上辅导费</a></li>
+
+ </ul>
+ </li>
+
+ </ul>
+
+ <form class="navbar-search pull-left" action="/goto" method="post" id="goto">
+ <input type="text" class="search-query span2" placeholder="Room To Go" name="id">
+ <input name="authenticity_token" type="hidden" value="19asKlq5UXru64BtMDq9thRKXghdzvkuB9zYdAe1pPY=" />
+ </form>
+
+ <p class="navbar-text">
+ <ul class="nav nav-pills pull-right">
+ <li class="dropdown" id="menu1">
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#">
+ small_fish__
+ <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu">
+ <li><a href="/users/202">Home Page</a></li>
+ <li><a href="/users/202/edit">Profile Settings</a></li>
+ <li class="divider"></li>
+ <li><a href="/sign_out">Logout</a></li>
+ </ul>
+ </li>
+ </ul>
+ </p>
+ </div>
+ </div>
+</div>
View
23 app/views/rooms/index.html.erb
@@ -0,0 +1,23 @@
+<div class="container-fluid" style="margin-top:50px;">
+ <div class="row-fluid">
+ <ul class='thumbnails'>
+ <%@rooms.each_with_index do |room,index|%>
+ <li class='room_list'>
+ <h5>
+ <%=link_to room.title,chat_room_path(room),target:'_blank'%>
+ <a class='like unfav' title='Fav It'>❤</a>
+ </h5>
+ <p class='desc'><%=room.description%></p>
+ <p class='stats'>
+ <a class='left' title='Online Users Count' >9</a>
+ <a class='right' title='Messages Count'>12</a>
+ </p>
+ </li>
+ <%end%>
+ </ul>
+ <%= will_paginate @rooms %>
+ </div>
+</div>
+<script type="text/javascript">
+ $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
+</script>
View
64 app/views/rooms/show.html.erb
@@ -0,0 +1,64 @@
+<div class="container-fluid">
+ <div class="row-fluid">
+ <div class="span9">
+ <p class='alert alert-success welcome-message'>
+ Welcome to the
+ <span id="room_title"><%=@room.title%></span>
+ chat room
+ <span id="user_name"><%= session[:username] %>!</span>
+ </p>
+ <div class="messages">
+ </div>
+ <div id="input-area">
+ <textarea id="message" class="input-xlarge"></textarea>
+ <button class="btn btn-success" id="submit">Send</button>
+ <span class="notice">支持markdown</span>
+ </div>
+ </div>
+ <div class="span2">
+ <div class='well sidebar-nav fenxiang' >
+ <h3 class='nav-header'>喜欢我就分享我吧</h3>
+ <div class='fenxiang-button my_border'>
+ <div class="jiathis_style_24x24">
+ <a class="jiathis_button_qzone"></a>
+ <a class="jiathis_button_tsina"></a>
+ <a class="jiathis_button_renren"></a>
+ <a class="jiathis_button_weixin"></a>
+ <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
+ </div>
+ <script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=1353687894005453" charset="utf-8"></script>
+ </div>
+ </div>
+ <div class='well sidebar-nav'>
+ <h3 class='nav-header'>主题</h3>
+ <p class='subject my_border'>
+ <%=@room.description%>
+ </p>
+ </div>
+ </div>
+ </div>
+</div>
+
+<script>
+ $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
+ $('.messages').css('height',window.screen.height-285);
+ $('.subject').css('max-height',window.screen.height-550);
+ $('#submit').click(function(){
+ faye.publish('/room/<%=@room.id%>/messages/new', {
+ username: '<%= session[:username] %>',
+ msg: $('#message').val()
+ });
+ $('#message').val('');
+ return false;
+ });
+ faye.subscribe("/room/<%=@room.id%>/messages/new",function(data){
+ var a_new_message = ('<li><div class="user-avatar"> <img src="'+'http://l.ruby-china.org/user/large_avatar/827.jpg"');
+ a_new_message +='/></div><div class="info"><span class="username">';
+ a_new_message +=data.username;
+ a_new_message +=('</span><span >'+(parseInt($('.messages li').size())+1)+' 楼</span>');
+ a_new_message +=('<span >'+ current_time()+'</span>');
+ a_new_message +=('<p>'+data.msg+'</p></div></li>');
+ $('.messages').append(a_new_message);
+ });
+</script>
+
View
5 config/environment.rb
@@ -1,5 +1,10 @@
+#encoding:utf-8
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
MyChatter::Application.initialize!
+
+require 'will_paginate'
+WillPaginate::ViewHelpers.pagination_options[:previous_label] = '上一页'
+WillPaginate::ViewHelpers.pagination_options[:next_label] = '下一页'
View
6 config/routes.rb
@@ -1,6 +1,8 @@
MyChatter::Application.routes.draw do
+
get '/login' => 'sessions#new', :as => :login
post '/login' => 'sessions#create', :as => :login
- get '/chatroom' => 'chats#room', :as => :chat
- root :to => 'chats#room'
+ get '/chatrooms' => 'rooms#index', :as=> :chat_rooms
+ get '/chatrooms/(:id)'=> 'rooms#show', :as=>:chat_room
+ root :to => 'rooms#index'
end
View
10 db/migrate/20130407052150_create_rooms.rb
@@ -0,0 +1,10 @@
+class CreateRooms < ActiveRecord::Migration
+ def change
+ create_table :rooms do |t|
+ t.string :title
+ t.text :description
+
+ t.timestamps
+ end
+ end
+end
View
6 db/seeds.rb
@@ -5,3 +5,9 @@
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
+
+
+Room.destroy_all
+22.times do |index|
+ Room.create({title:"room with id #{index+1}",description:"the description of room-#{index+1}"})
+end
Something went wrong with that request. Please try again.