`
jianxm
  • 浏览: 46525 次
  • 性别: Icon_minigender_1
  • 来自: 南京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

ROR站内短信教程

阅读更多
前提: 安装 Rails 2.0.2

新建Rails项目
$ rails messenger

新建模型
$ 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

安装插件 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

设置表间关联
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)


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

修改路由
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

修改控制及页面
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>


具体还有一些删除、全部回复请参考原文
http://www.novawave.net/public/rails_messaging_tutorial.html
分享到:
评论
3 楼 司徒正美 2009-03-18  
named_scope就是那个scope-out!
2 楼 zhuangj 2008-08-19  
少了一个logged_in方法, 一个current_user对象
1 楼 cxhnihaoa 2008-07-10  
怎么没有源码下载,按照上面操作,有错识呢.

相关推荐

    ror实例

    在压缩包中的`rubyonrails.pdf`文件可能是Rails的官方文档、教程或某位专家的经验分享,通过阅读这个文件,你可以深入理解Rails的工作方式,学习如何创建和管理数据库、编写控制器、搭建路由、构建视图以及进行测试...

    ROR books 经典教程 入门 提高

    此标题表明该教程是关于 Ruby on Rails (ROR) 相关书籍的经典教程,适合初学者入门以及提高进阶的学习需求。这里可能存在着一定的理解偏差,因为文件中提供的具体内容更多地是关于 Ruby 语言本身而非 Rails 框架,但...

    RoR性能优化经验谈

    RoR(Ruby on Rails)是一种流行的开源Web开发框架,以其高效和简洁的代码著称。然而,随着网站规模的增长,性能优化成为必不可少的环节。在本文中,我们将探讨一些RoR性能优化的关键方面,主要基于JavaEye网站在...

    ror

    NULL 博文链接:https://xuxiangpan888.iteye.com/blog/266696

    ror中文资料

    在提供的压缩包文件中,我们可以看到"Ruby语言中文教程",这暗示了资源可能包含了关于Ruby语言的基础知识和进阶内容,对于学习RoR至关重要。Ruby是RoR的基础,理解其语法和特性对于掌握RoR框架极其关键。 **Ruby...

    神经网络ror resenet模型

    **神经网络Ror ResNet模型详解** 在深度学习领域,ResNet(残差网络)模型是具有里程碑意义的创新,由He et al.在2015年提出。该模型解决了深度神经网络训练中的梯度消失问题,允许构建非常深的网络结构。而“Ror”...

    初探ROR

    **初探ROR** Ruby on Rails(简称ROR)是一个基于Ruby编程语言的开源Web应用程序框架,它遵循MVC(模型-视图-控制器)架构模式,旨在促进开发过程的简洁性和效率。Ruby on Rails的核心理念是“Don't Repeat ...

    ROR安装必备所有架包

    在Ruby on Rails(ROR)开发环境中,安装和配置正确的依赖包是至关重要的。这个压缩包包含了一系列用于ROR框架的基础组件,但不包括Ruby本身。让我们深入了解一下这些包的作用和重要性。 首先,`actionpack`是Rails...

    RoR选题方向—源代码

    Ruby on Rails(RoR)是一种基于Ruby语言的开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web开发过程。在这个选题方向中,我们主要探讨的是与RoR相关的源代码分析和学习。源代码是...

    RoRBlog 基于RoR的博客系统

    基于RoR的博客系统,代码风格简单清晰,前后太完善,适合初学者。

    freemis 基于ror框架的mis

    5. **安全性**:RoR内置了多种安全机制,如CSRF(跨站请求伪造)防护、XSS(跨站脚本)防护等,有助于保护系统的数据安全。 **学习和使用FreeMIS:** 对于想要深入理解或使用FreeMIS的开发者,首先需要掌握Ruby语言...

    ROR 文件的上传与下载

    ### ROR 文件的上传与下载:深入解析与实践 在Ruby on Rails(简称ROR)框架下,处理文件的上传与下载是一项常见的需求,尤其是在构建包含媒体内容的应用程序时。本文将基于给定的文件信息,详细阐述如何在Rails...

    Windows 上搭建 ROR环境

    ### Windows上搭建Ruby on Rails(ROR)环境详解 #### 一、引言 随着Web开发技术的不断发展,Ruby on Rails(简称Rails或ROR)作为一种高效、简洁且优雅的Web开发框架,受到了广大开发者的青睐。然而,在Windows...

    ROR绿色最新环境(2013/3/10)

    ROR环境 Ruby version 1.9.3 (java) RubyGems version 1.8.24 Rack version 1.4 Rails version 3.2.12 JavaScript Runtime therubyrhino (Rhino) Active Record version 3.2.12 Action Pack version 3.2.12 ...

    RoR,十分钟做Blog

    【RoR,十分钟做Blog】这篇教程主要介绍了如何使用Ruby on Rails(RoR)框架在NetBeans IDE上快速创建一个简单的博客程序。RoR是一个基于MVC(模型-视图-控制器)架构的Web开发框架,它使得开发过程更加高效且简洁。...

    RoR 培训课程PPT

    - **书籍推荐**:《Agile Web Development with Rails》是一本经典的RoR教程,适合进一步深入了解RoR的开发实践。 - **在线社区**:参与Ruby on Rails官方论坛或Stack Overflow等社区,获取最新的技术资讯和解答开发...

    javarebel 用JAVA和ROR一样方便测试

    破解版本的JAR包,放到本地磁盘,ECLIPUS直接用,到JVM设置直接加 -noverify -javaagent:D:\javarebel.jar

    机遇ROR 的图书管理系统

    《机遇ROR的图书管理系统》是一份以Ruby on Rails(简称ROR)技术为核心,旨在构建高效、便捷的图书管理解决方案的学习资料。Ruby on Rails,是基于Ruby编程语言的开源Web应用框架,它遵循MVC(Model-View-...

    ROR环境配置

    在IT行业中,Ruby on Rails(简称ROR)是一款基于Ruby语言的开源Web应用程序框架,它遵循MVC(Model-View-Controller)架构模式,旨在简化Web应用开发过程,提高开发效率。本文将深入探讨如何配置ROR开发环境,以及...

Global site tag (gtag.js) - Google Analytics