乐者为王

Do one thing, and do it well.

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

今天我们来继续完善我们的记账应用。

首先,修改app/views/home/index.html.erb为如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
<h1>Welcom to Qianbao App!</h1>

<% if logged_in? %>
  <p><strong>You are logged in as <%=h current_user.login %></strong></p>
  <p><%= link_to 'Logout', logout_path %></p>
<% else %>
  <p><strong>You are currently not logged in.</strong></p>
  <p>
    <%= link_to 'Login', login_path %> or
    <%= link_to 'Sign up', signup_path %>
  </p>
<% end %>

要在视图中使用logged_in?和current_user这两个帮助器方法,还需要在app/controllers/application_controller.rb中增加引入AuthenticatedSystem语句:

1
2
3
4
5
6
7
8
9
class ApplicationController < ActionController::Base
  include AuthenticatedSystem

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
end

否则会报告下面的错误:

1
undefined method 'logged_in?'

既然在ApplicationController中已经包含了该模块,那么其它控制器中的也就可以删除了。

然后创建app/views/users/create.html.erb文件:

1
2
3
4
5
6
7
8
9
10
11
<h1>Please confirm your registration!</h1>

<p>
  A mail has been sent to <%= @user.email %> with instructions to activate your account.
  <li>
    If your email is not valid, you must <%= link_to "Sign up", signup_path %> again and provide a valid email address.
  </li>
  <li>
    If you don't recieve an email, check your bulk or trash folder, as your spam filter may have inadvertantly caught the registration email.
  </li>
</p>

同时注释掉app/controllers/users_controller.rb中create方法中的这行代码:

1
redirect_back_or_default('/')

这样当用户注册成功后就不会重定向到首页,而是显示提醒用户收取激活帐号邮件的页面。

另外,还需要把entries保护起来,不能让未登录的用户访问。可以在app/controllers/entries_controller.rb中添加一个before_filter做到:

1
2
3
4
5
6
class EntriesController < ApplicationController
  before_filter :login_required

  # GET /entries
  # GET /entries.xml
  def index

还要将登录后的页面改成entries列表,这需要将app/controllers/sessions_controller.rb中create方法下的:

1
redirect_back_or_default('/')

改为

1
redirect_back_or_default('/entries')

最后,还要调整下entries各个页面之间的流转顺序,把show这个页面去掉,使得在添加或更新记录后就直接跳到entries列表,而不再是show页面。

  1. 先将app/views/entries/show.html.erb删除;
  2. 删除app/views/entries/index.html.erb中的show链接;
  3. 删除app/views/entries/edit.html.erb中的show链接;
  4. 然后删除app/controllers/entries_controller.erb中的show方法;
  5. 再修改app/controllers/entries_controller.erb中的update和create方法为以下代码:
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
def create
  @entry = Entry.new(params[:entry])
  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 = Entry.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

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

Comments