ROR站内短信教程

前提: 安装 Rails 2.0.2

[b]新建Rails项目[/b]

$ rails messenger

[b]新建模型[/b]

$ script/generate model message author_id:integer subject:string body:text
$ script/generate model message_copy recipient_id:integer message_id:integer folder_id:integer
$ script/generate model folder user_id:integer parent_id:integer name:string

[b]安装插件[/b] restful_authentication, scope_out, acts_as_tree and will_paginate

$ script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication
$ script/generate authenticated user sessions
$ script/plugin install acts_as_tree
$ script/plugin install http://scope-out-rails.googlecode.com/svn/trunk/
$ script/plugin install svn://errtheblog.com/svn/plugins/will_paginate
$ rake db:migrate

[b]设置表间关联[/b]

class Message < ActiveRecord::Base
belongs_to :author, :class_name => "User"
has_many :message_copies
has_many :recipients, :through => :message_copies
before_create :prepare_copies

attr_accessor :to # array of people to send to
attr_accessible :subject, :body, :to

def prepare_copies
return if to.blank?

to.each do |recipient|
recipient = User.find(recipient)
message_copies.build(:recipient_id => recipient.id, :folder_id => recipient.inbox.id)
end
end
end


class MessageCopy < ActiveRecord::Base
belongs_to :message
belongs_to :recipient, :class_name => "User"
belongs_to :folder
delegate :author, :created_at, :subject, :body, :recipients, :to => :message
end


class Folder < ActiveRecord::Base
acts_as_tree
belongs_to :user
has_many :messages, :class_name => "MessageCopy"
end

修改User模型

has_many :sent_messages, :class_name => "Message", :foreign_key => "author_id"
has_many :received_messages, :class_name => "MessageCopy", :foreign_key => "recipient_id"
has_many :folders

before_create :build_inbox

def inbox
folders.find_by_name("收件箱")
end

def build_inbox
folders.build(:name => "收件箱")
end
# (Autogenerated restful_authentication code remains here)


[b]新建控制及页面[/b]
$ script/generate controller sent index show new
$ script/generate controller messages show
$ script/generate controller mailbox show

[b]修改路由[/b]
ActionController::Routing::Routes.draw do |map|
map.resources :users, :sent, :mailbox
map.resources :messages, :member => { :reply => :get }
map.resource :session

# Home route leads to inbox
map.inbox '', :controller => "mailbox", :action => "index"

# Install the default routes as the lowest priority.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

[b]修改控制及页面[/b]

class SentController < ApplicationController
def index
@messages = current_user.sent_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end

def show
@message = current_user.sent_messages.find(params[:id])
end

def new
@message = current_user.sent_messages.build
end

def create
@message = current_user.sent_messages.build(params[:message])

if @message.save
flash[:notice] = "Message sent."
redirect_to :action => "index"
else
render :action => "new"
end
end
end


=== file: app/views/layouts/application.html.erb ===

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Rails Messenger</title>
</head>

<body>
<h1>Rails Messenger</h1>

<% if flash[:notice] %>
<p style="color:green"><%= flash[:notice] %></p>
<% end %>

<% if flash[:error] %>
<p style="color:red"><%= flash[:error] %></p>
<% end %>

<% if logged_in? %>
<p>Welcome, <%=h current_user.login %>. <%= link_to "Logout", session_path, :method => "delete" %></p>
<% else %>
<p>You are not logged in. <%= link_to "Register", new_user_path %> or <%= link_to "Login", new_session_path %></p>
<% end %>

<%= render :partial => "layouts/mailbox_list" if logged_in? %>

<%= yield %>
</body>
</html>

=== file: app/views/layouts/_mailbox_list.html.erb ===

<div id="mailbox_list" style="border:1px solid #aaa; float:right; margin:1em; padding:1em; width:20%">
<p><%= link_to "Compose", new_sent_path %></p>

<p><strong>Mailboxes</strong></p>
<ul>
<li><%= link_to "Inbox", inbox_path %></li>
<li><%= link_to "Sent", :controller => "sent", :action => "index" %></li>
</ul>
</div>

=== file: app/views/sent/new.html.erb ===

<h2>Compose</h2>

<% form_for :message, :url => {:controller => "sent", :action => "create"} do |f| %>

<p>
To:<br />
<select name="message[to][]">
<%= options_from_collection_for_select(User.find(:all), :id, :login, @message.to) %>
</select>
</p>

<p>Subject: <%= f.text_field :subject %></p>
<p>Body:<br /> <%= f.text_area :body %></p>
<p><%= submit_tag "Send" %></p>
<% end %>

=== file: app/views/sent/index.html.erb ===

<h2>Sent Messages</h2>

<table border="1">
<tr>
<th>To</th>
<th>Subject</th>
<th>Sent</th>
</tr>

<% for message in @messages %>
<tr>
<td><%=h message.recipients.map(&:login).to_sentence %></td>
<td><%= link_to h(message.subject), sent_path(message) %></td>
<td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
</tr>
<% end %>
</table>

<%= will_paginate @messages %>

=== file: app/views/sent/show.html.erb ===

<h2>Sent: <%=h(@message.subject) %></h2>
<p><strong>To:</strong> <%= @message.recipients.map(&:login).to_sentence %></p>
<p><strong>Sent:</strong> <%= @message.created_at.to_s(:long) %></p>

<pre><%=h @message.body %></pre>

=== file: app/controllers/mailbox_controller.rb ===

class MailboxController < ApplicationController
def index
redirect_to new_session_path and return unless logged_in?
@folder = current_user.inbox
show
render :action => "show"
end

def show
@folder ||= current_user.folders.find(params[:id])
@messages = @folder.messages.paginate :per_page => 10, :page => params[:page], :include => :message, :order => "messages.created_at DESC"
end
end
=== file: app/vies/mailbox/show.html.erb ===

<h2><%=h @folder.name %></h2>

<table border="1">
<tr>
<th>From</th>
<th>Subject</th>
<th>Received</th>
</tr>

<% for message in @messages %>
<tr>
<td><%=h message.author.login %></td>
<td><%= link_to h(message.subject), message_path(message) %></td>
<td><%= distance_of_time_in_words(message.created_at, Time.now) %> ago</td>
</tr>
<% end %>
</table>

<%= will_paginate @messages %>


=== file: app/controllers/messages_controller.rb ===
class MessagesController < ApplicationController
def show
@message = current_user.received_messages.find(params[:id])
end

def reply
@original = current_user.received_messages.find(params[:id])

subject = @original.subject.sub(/^(Re: )?/, "Re: ")
body = @original.body.gsub(/^/, "> ")
@message = current_user.sent_messages.build(:to => [@original.author.id], :subject => subject, :body => body)
render :template => "sent/new"
end
end

=== file: app/views/messages/show.html.erb ===

<h2><%=h @message.subject %></h2>

<p><strong>From:</strong> <%=h @message.author.login %></p>
<p><strong>To:</strong> <%=h @message.recipients.map(&:login).to_sentence %></p>
<p><strong>Received:</strong> <%= @message.created_at.to_s(:long) %></p>

<pre><%=h @message.body %></pre>

=== file: app/views/messages/show.html.erb ===

<h2><%=h @message.subject %></h2>

<p><strong>From:</strong> <%=h @message.author.login %></p>
<p><strong>To:</strong> <%=h @message.recipients.map(&:login).to_sentence %></p>
<p><strong>Received:</strong> <%= @message.created_at.to_s(:long) %></p>

<pre><%=h @message.body %></pre>

<p><%= link_to "Reply", reply_message_path(@message) %></p>


具体还有一些删除、全部回复请参考原文
[url]http://www.novawave.net/public/rails_messaging_tutorial.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值