乐者为王

Do one thing, and do it well.

用Rails 2.3打造简单记账应用(6)

调整后的entries页面就是不一样,感觉清爽多了。现在让我们添加几条测试数据吧!进入new页面,感觉似乎少了点什么。对了,怎么没有消费发生的具体时间呢?还是先把这个给加上吧。

执行下面的命令,创建一个迁移任务,添加date类型的effective_date字段到entries中:

1
2
script/generate migration add_effective_date_to_entries effective_date:date
rake db:migrate

然后修改app/views/entries/index.html.erb以显示该字段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<h1>Listing entries</h1>

<table>
  <tr>
    <th>Date</th>
    <th>Amount</th>
    <th>Tags</th>
    <th>Comment</th>
    <th>Action</th>
  </tr>

  <% @entries.each do |entry| %>
  <tr>
    <td><%=h entry.effective_date %></td>
    <td><%=h entry.amount %></td>
    <td><%=h entry.tags %></td>
    <td><%=h entry.comment %></td>
    <td>
      <%= link_to 'Edit', edit_entry_path(entry) %>
      <%= link_to 'Destroy', entry, :confirm => 'Are you sure?', :method => :delete %>
    </td>
  </tr>
  <% end %>
</table>

<%= link_to 'New entry', new_entry_path %>

修改new和edit页面,加上一个effective_date文本字段:

1
2
3
4
5
6
7
<% form_for(@entry) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :effective_date %><br />
    <%= f.date_select :effective_date %>
  </p>

录入数据后发现记录是全局的,也就是说A用户添加的记录B用户也能看到,这是个问题。

用户和记录是一对多的关系,可以通过以下步骤把它们关联起来。

1
2
script/generate migration add_user_id_to_entries user_id:integer
rake db:migrate

修改app/models/user.rb,添加以下代码:

1
has_many :entries, :dependent => :destroy

修改app/models/entry.rb,加入下面的语句:

1
belongs_to :user

另外还要修改app/controllers/entries_controller.rb文件,修正由scaffold生成代码所带来的查询问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class EntriesController < ApplicationController
  before_filter :login_required

  def index
    @entries = current_user.entries.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @entries }
    end
  end

  def new
    @entry = Entry.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @entry }
    end
  end

  def edit
    @entry = current_user.entries.find(params[:id])
  end

  def create
    @entry = Entry.new(params[:entry])
    @entry.user = current_user

    respond_to do |format|
      if @entry.save
        flash[:notice] = 'Entry was successfully created.'
        format.html { redirect_to(entries_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @entry = current_user.entries.find(params[:id])

    respond_to do |format|
      if @entry.update_attributes(params[:entry])
        flash[:notice] = 'Entry was successfully updated.'
        format.html { redirect_to(entries_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    @entry = current_user.entries.find(params[:id])
    @entry.destroy

    respond_to do |format|
      format.html { redirect_to(entries_url) }
      format.xml  { head :ok }
    end
  end
end

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

Comments