乐者为王

Do one thing, and do it well.

用Rails 2.3打造todolist应用

首先生成项目骨架:

1
2
3
4
rails todolist
cd todolist
script/generate scaffold todo title:string description:text done:boolean due_date:datetime
rake db:migrate

安装认证和授权插件:

1
2
3
4
5
script/plugin install git://github.com/technoweenie/restful-authentication.git restful_authentication
script/generate authenticated user sessions

script/plugin install git://github.com/greenisus/forgot_password.git
script/generate forgot_password password user

然后将include AuthenticatedSystem移到ApplicationController中:

1
2
class ApplicationController < ActionController::Base
  include AuthenticatedSystem

添加Todo和User的关联:

1
2
3
4
5
class Todo < ActiveRecord::Base
  belongs_to :user

class User < ActiveRecord::Base
  has_many :todos

修改TodosController,将Todo和User绑定:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class TodosController < ApplicationController
  before_filter :login_required

  def index
    @todos = current_user.todos

  def show
    @todo = current_user.todos.find(params[:id])

  def new
    @todo = Todo.new

  def edit
    @todo = current_user.todos.find(params[:id])

  def create
    @todo = Todo.new(params[:todo])
    @todo.user = current_user

  def update
    @todo = current_user.todos.find(params[:id])

  def destroy
    @todo = current_user.todos.find(params[:id])

创建一个应用的首页:

1
script/generate controller home index

为了可以访问到应用首页,需要删除public/index.html文件,并且在routes.rb中添加:

1
map.root :controller => 'home'

将app/views/layouts下的todos.html.erb改名为application.html.erb,然后添加下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<% if logged_in? -%>
  <div id="user-bar-greeting">
    Logged in as <%= link_to_current_user :content_method => :login %>
  </div>
  <div id="user-bar-action">
    (<%= link_to "Log out", logout_path, { :title => "Log out" } %>)
  </div>
<% else -%>
  <div id="user-bar-action">
    <%= link_to "Log in", login_path, { :title => "Log in" } %> /
     <%= link_to "Sign up", signup_path, { :title => "Create an account" } %>
  </div>
<% end -%>

Todo模型的description字段是text类型,在页面上用textarea表示简单了一点,将它改成使用TinyMCE编辑器。可以使用tinymce_hammer插件来集成。

1
script/plugin install git://github.com/trevorrowe/tinymce_hammer.git

执行以下命令后,将会安装TinyMCE到public/javascript/tiny_mce目录:

1
script/generate tinymce_installation

在layout下的模板中添加下面这条语句:

1
<%= init_tinymce_hammer_if_required %>

将app/views/todos下new.html.erb和edit.html.erb中的

1
<%= f.text_area :description %>

修改为

1
<%= f.tinymce :description, :rows => 10, :cols => 40 %>

去除app/views/todos/index.html.erb中description字段的h方法:

1
<td><%= todo.description %></td><br />

还有就是删除某条Todo后记录就彻底地没了,不能恢复,需要给它加上一剂后悔药,使之可以重新被捞出,acts_as_paranoid插件可以做到这点。

1
2
3
script/plugin install git://github.com/technoweenie/acts_as_paranoid.git
script/generate migration add_deleted_at_to_todos deleted_at:datetime
rake db:migrate

添加acts_as_paranoid到模型中:

1
2
class Todo < ActiveRecord::Base
  acts_as_paranoid

现在,调用这个模型的destroy方法将不会真正地删除记录,只会将记录从视图上移除,在deleted_at里记录删除的时间。当然,你可以在find中使用with_deleted或only_deleted参数得到被隐藏的记录。在Rails 3中在find中使用参数会报ArgumentError,显示如下错误:

1
Unknown key: only_deleted

解决办法是使用以下格式的代码:

1
current_user.todos.only_deleted.find(:all)

至此,一个简单的todolist就算完成了。

代码下载:https://github.com/dohkoos/todolist

Comments