Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

基本完成room的增删改查 #18

Merged
merged 1 commit into from

1 participant

@songjiayang

需要执行数据迁移

@songjiayang songjiayang merged commit 1eee008 into fireweb:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 9, 2013
  1. @songjiayang

    完成room的增删改查

    songjiayang authored
This page is out of date. Refresh to see the latest.
View
112 app/assets/stylesheets/room.css.erb
@@ -213,3 +213,115 @@
max-width: 400px;
max-height: 400px;
}
+
+
+.room_form{
+ width: 80%;
+ background-color: #fff;
+ height: 400px;
+ margin-top: 100px;
+ margin-left: 70px;
+ display: block;
+ padding: 4px;
+ position:relative;
+ text-overflow:ellipsis;
+ border: 1px solid #DDD;
+ border-radius: 20px;
+
+}
+
+.my_room_list{
+ width: 100%;
+ background-color: #fff;
+ margin-top: 100px;
+ margin-left: -70px;
+ display: block;
+ padding: 4px;
+ height:400px;
+ position:relative;
+ text-overflow:ellipsis;
+ border: 1px solid #DDD;
+ border-radius: 20px;
+
+}
+
+.my_room_list_li{
+ float: left;
+ width: 100%;
+ margin: 4px;
+ height:300px;
+ position:relative;
+ overflow: auto;
+}
+
+
+.form-description{
+ margin-left: 20%;
+ height: 100px;
+ width: 400px;
+ overflow: auto;
+}
+
+.room-form p {
+ margin-left: 20%;
+ width: 360px;
+ display: none;
+}
+
+.room-form #room_title{
+ margin-left: 20%;
+ width: 400px;
+ height: 30px;
+}
+
+.room-form .control-group{
+ padding-top: 10px;
+}
+
+.room-form .action{
+ margin-left: 20%;
+}
+
+.room-form .action a{
+ margin-left: 5%;
+}
+
+#rooms{
+ width: 70%;
+ background-color: #fff;
+ margin-top: 100px;
+ margin-left: 12%;
+ display: block;
+ padding: 4px;
+ height:450px;
+ position:relative;
+ text-overflow:ellipsis;
+ border: 1px solid #DDD;
+ border-radius: 20px;
+ overflow: auto;
+}
+
+#rooms table{
+ margin: 0px;
+}
+
+#rooms .room_title{
+ width: 50%;
+}
+
+
+#rooms .pagination{
+ float: left;
+ position: absolute;
+ margin-top: 420px;
+ margin-left: 35%;
+}
+
+#room_message{
+ float: left;
+ width: 30%;
+ position: absolute;
+ margin-top: -45px;
+ margin-left: 12%;
+}
+
View
43 app/controllers/rooms_controller.rb
@@ -1,4 +1,4 @@
-
+#encoding:utf-8
class RoomsController < ApplicationController
before_filter :authcation_with_user
@@ -12,8 +12,45 @@ def show
end
def sent_message
- respond_to do |format|
- format.json { render json:$markdown.render(params[:msg])}
+ render json:$markdown.render(params[:msg])
+ end
+
+ def new
+ @room = Room.new
+ end
+
+ def create
+ current_user.rooms.create(params[:room])
+ redirect_to chat_rooms_path
+ end
+
+ def rooms
+ @rooms = Room.where('user_id=?',current_user.id).order("id desc").paginate(:page => params[:page], :per_page => 10)
+ end
+
+ def edit
+ @room = Room.where('user_id=? and id=?',current_user.id,params[:id]).first
+ end
+
+ def update
+ @room = Room.where('user_id=? and id=?',current_user.id,params[:id]).first
+ if @room and @room.update_attributes(params[:room])
+ flash[:success] = "编号为#{params[:id]}的房间修改成功!"
+ redirect_to my_room_path
+ else
+ flash[:error] = "编号为#{params[:id]}的房间修改失败!"
+ redirect_to my_room_path
+ end
+ end
+
+ def destroy
+ @room = Room.where('user_id=? and id=?',current_user.id,params[:id]).first
+ if @room and @room.destroy
+ flash[:success] = "编号为#{params[:id]}的房间删除成功!"
+ redirect_to my_room_path
+ else
+ flash[:error] = "编号为#{params[:id]}的房间删除失败!"
+ redirect_to my_room_path
end
end
end
View
1  app/models/room.rb
@@ -1,4 +1,5 @@
class Room < ActiveRecord::Base
attr_accessible :description, :title
validates :description, :title, :presence => true
+ belongs_to :user
end
View
1  app/models/user.rb
@@ -4,6 +4,7 @@ class User < ActiveRecord::Base
attr_accessible :email, :username
validates :email, :uniqueness => true
validates :email, :presence => true
+ has_many :rooms, :dependent=>:destroy
def self.image_with_email(email,username)
user = User.find_by_email(email.to_s)|| User.create({username:username.to_s,email:email.to_s})
View
3  app/views/layouts/application.html.erb
@@ -12,5 +12,8 @@
<body>
<%=render 'rooms/topbar'%>
<%= yield %>
+<script type="text/javascript">
+ $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
+</script>
</body>
</html>
View
58 app/views/rooms/_form.html.erb
@@ -0,0 +1,58 @@
+
+
+<div class="container-fluid">
+ <div class="row-fluid">
+ <div class="span9">
+ <div class='room_form'>
+ <%= form_for @room do |f|%>
+ <fieldset class='room-form'>
+ <legend>新建房间</legend>
+ <p class="alert">
+ <a class="close" >×</a>
+ <span id='alert-message'><%=flash[:error]%></span>
+ </p>
+ <div class="control-group">
+ <div class="controls">
+ <%= f.text_field :title, class:"input-small",placeholder:"标题"%>
+ </div>
+ </div>
+ <div class="control-group">
+ <div class="controls">
+ <%= f.text_area :description, class:"form-description",placeholder:"描述"%>
+ </div>
+ </div>
+
+ <div class="control-group">
+ <div class="controls action">
+ <%= f.submit 'submit', class:'btn btn-success', id:'room-form-submit'%>
+ <%=link_to 'back',chat_rooms_path, class:'btn btn-info'%>
+ </div>
+ </div>
+ </fieldset>
+ <%end%>
+ </div>
+ </div>
+ <div class="span3">
+ <%= render :partial => "my_room_list"%>
+ </div>
+ </div>
+</div>
+
+
+
+<script type="text/javascript">
+
+ $('#room-form-submit').click(function(){
+ if($('#room_title').val().length<1||$('#room_description').val().length<1){
+ $('#alert-message').text('标题和描述不能为空');
+ $('.alert').show();
+ return false;
+ }else{
+ return true;
+ }
+ });
+ $('.close').click(function(){
+ $('.alert').hide();
+ });
+ if($('#alert-message').text().length>0) {$('.alert').show()};
+</script>
View
8 app/views/rooms/_my_room_list.html.erb
@@ -0,0 +1,8 @@
+ <div class='my_room_list' >
+ <legend>我的房间</legend>
+ <div class='my_room_list_li'>
+ <% current_user.rooms.reverse.each do |room|%>
+ <li> <%=link_to truncate(room.title,:length => 45, :omission => '... '), (chat_room_path room),class:'link_to'%></li>
+ <%end%>
+ </div>
+ </div>
View
25 app/views/rooms/_topbar.html.erb
@@ -3,16 +3,14 @@
<div class="container-fluid">
<a class="brand" href=/>Chater</a>
<ul class="nav top-bar-ul">
- <li class="active"><a href="/">Home</a></li>
- <li><a href="/rooms/new" data-remote="true">New Room</a></li>
+ <li class="<%='active' if action_name=='index'%>"><a href="/">Home</a></li>
+ <li class="<%='active' if action_name=='new'%>"><%=link_to 'New Room', new_chat_room_path%></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>
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#fat">My Favs
+ <b class="caret"></b></a>
+ <ul class="dropdown-menu fav" id="top_bar_rooms">
+ 暂无
</ul>
</li>
<li class="divider-vertical"></li>
@@ -21,8 +19,14 @@
My Rooms
<b class="caret"></b>
</a>
- <ul class="dropdown-menu fav" id="fav_rooms">
- <li><a href="/rooms/41">上辅导费</a></li>
+ <ul class="dropdown-menu fav" id="top_bar_rooms">
+ <% current_user.rooms.last(5).each do |room|%>
+ <li> <%=link_to truncate(room.title,:length => 25, :omission => '... '), (chat_room_path room)%></li>
+ <%end%>
+ <% if current_user.rooms.size >5%>
+ <li class="divider"></li>
+ <li><%=link_to '更多',my_room_path%></li>
+ <%end%>
</ul>
</li>
@@ -43,6 +47,7 @@
<ul class="dropdown-menu">
<li><%=link_to '个人主页',user_path(current_user)%></li>
<li><%=link_to '信息设置', edit_user_path(current_user) %></li>
+ <li><%=link_to '我的房间', my_room_path%></li>
<li class="divider"></li>
<li><%=link_to '退出',logout_path%></li>
</ul>
View
1  app/views/rooms/edit.html.erb
@@ -0,0 +1 @@
+<%= render :partial => "form"%>
View
6 app/views/rooms/index.html.erb
@@ -5,13 +5,13 @@
<%@rooms.each_with_index do |room,index|%>
<li class='room_list'>
<h5>
- <%=link_to room.title,chat_room_path(room),target:'_blank'%>
+ <%=link_to truncate(room.title,:length => 25, :omission => '... '),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>
+ <a class='left' title='Online Users Count' >0</a>
+ <a class='right' title='Messages Count'>0</a>
</p>
</li>
<%end%>
View
1  app/views/rooms/new.html.erb
@@ -0,0 +1 @@
+<%= render :partial => "form" %>
View
45 app/views/rooms/rooms.html.erb
@@ -0,0 +1,45 @@
+<div id='room_message'>
+ <%if flash[:error]%>
+ <div class='alert alert-error'>
+ <a class='close' data-dismiss='alert' href='#'>×</a>
+ <%=flash[:error]%>
+ </div>
+ <%elsif flash[:success] %>
+ <div class='alert alert-success'>
+ <a class='close' data-dismiss='alert' href='#'>×</a>
+ <%=flash[:success]%>
+ </div>
+ <%end%>
+</div>
+
+<div id="rooms">
+ <table class="table table-condensed table-striped">
+ <thead>
+ <th class="header pjax headerSortDown id_field integer_type">编号</th>
+ <th class=" commentable_field polymorphic_association_type">标题</th>
+ <th class="header pjax created_at_field datetime_type">创建时间</th>
+ <th class="header pjax created_at_field datetime_type">收藏人数</th>
+ <th class="last shrink"></th>
+ </tr>
+ </thead>
+<tbody>
+ <%@rooms.each do |room|%>
+ <tr>
+ <td><%=room.id%></td>
+ <td class='room_title'><%=link_to truncate(room.title,:length => 65, :omission => '... '), chat_room_path(room)%></td>
+ <td><%=room.created_at.localtime.to_s(:db)%></td>
+ <td>0</td>
+ <td class="last links">
+ <ul class="inline">
+ <%=link_to raw('<i class="icon-info-sign"></i>'),chat_room_path(room),title:'查看'%>
+ <%=link_to raw('<i class="icon-pencil"></i>'),edit_room_path(room),title:'修改'%>
+ <%= link_to raw('<i class="icon-remove"></i>'), room_path(room), method: :delete, data: { confirm: 'Are you sure?' } , title: '删除' %>
+ </ul>
+ </td>
+ </tr>
+ <%end%>
+</tbody>
+</body>
+ <%= will_paginate @rooms %>
+ </div>
+
View
1  app/views/rooms/show.html.erb
@@ -65,7 +65,6 @@
}
return false;
}
- $('body').css('background',"url(<%=asset_path 'bg2.jpg'%>) no-repeat fixed bottom");
$('.messages').css('height',window.screen.height-300);
$('.subject').css('max-height',window.screen.height-550);
$('#submit').click(function(){
View
5 config/routes.rb
@@ -4,7 +4,10 @@
post '/login' => 'sessions#create', :as => :login
get 'logout' => 'sessions#destroy', :as =>:logout
get '/chatrooms' => 'rooms#index', :as=> :chat_rooms
- get '/chatrooms/(:id)'=> 'rooms#show', :as=>:chat_room
+ get '/chatrooms/(:id)'=> 'rooms#show', :as=>:chat_room, :constraints => { :id => /\d+/ }
+ get '/chatrooms/new'=> 'rooms#new', :as=>:new_chat_room
+ get '/chatrooms/my'=> 'rooms#rooms', :as=>:my_room
+ resources :rooms, :only=>[:create,:edit,:update,:destroy]
post '/parse_message'=>'rooms#sent_message'
resources :users
root :to => 'rooms#index'
View
6 db/migrate/20130409123709_add_user_id_to_rooms.rb
@@ -0,0 +1,6 @@
+class AddUserIdToRooms < ActiveRecord::Migration
+ def change
+ add_column :rooms, :user_id, :integer
+ Room.update_all({user_id:(User.first.id)})
+ end
+end
Something went wrong with that request. Please try again.