`
peryt
  • 浏览: 55699 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • waiting: 既然都指定了dataType为'script'那就不必特别在b ...
    jQuery

10.3 showing users list

 
阅读更多

in this chapter, we will do user list, i.e. the index action of users controller.

at the same time, we will study pagination, and how to populate many sample data into database.

 

1. we will make sure sign-in user can see all users list.

non-signed-in user can only see users show page, not the all users list.

 

so we will write the following test to follow our lovely TDD.

 

  describe "GET 'index'" do
    describe "for non-signed-in users" do
      it "should deny access" do
        get :index
        response.should redirect_to signin_path
        flash[:notice].should =~ /sign in/i
      end
    end
    describe "for signed-in users" do
      before(:each) do
        @user = test_sign_in(Factory(:user))
        second = Factory(:user, :name => "bob", :email => "another@example.com")
        third = Factory(:user, :name => "sam", :email => "another@example.net")
        @user = [@user, second, third]
      end
      it "should be success" do
        get :index
        response.should be_success
      end
      it "should have the right title" do
        get :index
        response.should have_selector("title", :content => "All users")
      end
      it "should have an element for each user" do
        get :index
        @users.each do |user|
          response.should have_selector("li", :content => user.name)
        end
      end
    end
  end

 We used Factory to generate 3 user object into database.

 

2. here is the code of index action:

 

 def index
    @title = "All users"
    @users = User.all
  end

 we also add :index to the before filter to make sure it need user to sign in first.

 

3. now, it is time to implement the index view:

 

<h1>All users</h1>

<ul class="users">
	<% @users.each do |user|%>
		<li>
			<%= gravatar_for user, :size => 30 %>
			<%= link_to user.name, user%>
		</li>
	<% end %>
</ul>

 

4. now we will add more sample data into database.

 

a. we need to add a gem to gemfile's development group.

  gem 'faker', '0.3.1'

 

below is the code in lib/tasks

sample_data.rake

 

namespace :db do
  desc "Fill database with sample data"
  task :populate => :environment do
    Rake::Task['db:reset'].invoke
    User.create!(:name => "Example User",
                 :email => "example@railstutorial.org",
                 :password => "foobar",
                 :password_confirmation => "foobar")
    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(:name => name,
                   :email => email,
                   :password => password,
                   :password_confirmation => password)
    end
  end
end

 this defines a db:populate task.

it firstly reset the dev database, 

task :populate => :environment,  make sure rake can use local rails env, so that it can use User.create method.

 

next, we can run

 

rake db:populate

 

5. pagination:

we will use the most simple and robust will_paginate gem to do this job.

add this gem to your gemfile

 

 

gem 'will_paginate', '3.0.pre2'


 

<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <% @users.each do |user| %>
    <li>
      <%= gravatar_for user, :size => 30 %>
      <%= link_to user.name, user %>
    </li>
  <% end %>
</ul>

<%= will_paginate %>

 will_paginate method is miracle, it will find the @users object, then display links to other pages.

but this part of code is not working yet, as the @users is from User.all, which is an array, but will_paginate require a WillPaginate:Collection

luckily, will_paginate provide a method to get Collection object from a class name.

 

User.paginate(:page => 1)

 

this method will pull a chunk of users out from database at a time, based on page param, 30 by default,  

for example, if page = 1, will pull 1-30

if page=2, will pull 31-60

...

 

6. next, we will test pagination:

(becaue we need to know how pagination works before testing it, so we implemented it first.)

we also need more then 30 users being populated into test database, so we will use Factory to do this.

 

Factory.define :user do |user|
  user.name                  "Michael Hartl"
  user.email                 "mhartl@example.com"
  user.password              "foobar"
  user.password_confirmation "foobar"
end

Factory.sequence :email do |n|
  "person-#{n}@example.com"
end

 this make use can use

Factory(:user, :email => Factory.next(:email))

 to get new emails.

 

require 'spec_helper'

describe "UsersController" do
  render_views

  describe "GET 'index'" do
    .
    .
    .
    describe "for signed-in users" do

      before(:each) do
        .
        .
        .
        @users = [@user, second, third]
        30.times do
          @users << Factory(:user, :email => Factory.next(:email))
        end
      end
      .
      .
      .
      it "should have an element for each user" do
        get :index
        @users[0..2].each do |user|
          response.should have_selector("li", :content => user.name)
        end
      end

      it "should paginate users" do
        get :index
        response.should have_selector("div.pagination")
        response.should have_selector("span.disabled", :content => "Previous")
        response.should have_selector("a", :href => "/users?page=2",
                                           :content => "2")
        response.should have_selector("a", :href => "/users?page=2",
                                           :content => "Next")
      end
    end
  end
  .
  .
  .
end

note

>> a = [1, 2, 5

>> a << 17 

>> a << 42 << 1337

so << operator to a array can be chained. 

 

7.

the different of span and div

   DIV  SPAN 元素最大的特点是默认都没有对元素内的对象进行任何格式化渲染。主要用于应用样式表。两者最明显的区别在于DIV块元素,而SPAN是行内元素(译作内嵌元素) 

  块元素和行内元素也不是一成不变的,通过定义CSSdisplay属性值可以互相转化,如: 

    测试<div style="display:inline">紧跟前面的"测试"显示</div><span style="display:block">这里会另起一行显示</span> 

 

8. partial refactoring.

as we have constitue test codes, so we are confident that our refactoring will not break the app.

 

9. we can use a render partial to replace the li part for displaying each user.

    <%= render user %>

 so rails is smart enough to deduce to put _user.html.erb partial here.

<li>
  <%= gravatar_for user, :size => 30 %>
  <%= link_to user.name, user %>
</li>

 and further more, we can just use:

render @users
rails is so smart that he know he should iterate the element in @users, each invode the partial once. and inside the partial, you already have a user object for use there.

分享到:
评论

相关推荐

    Arcgis 10.3 破解文件

    Arcgis 10.3破解文件,使用方法: 1、自行安装lm,关闭服务,拷贝破解文件①② 2、自行安装desktop,拷贝破解文件③,启动lm服务。 破解覆盖文件所在路径: ①service.txt---&gt;C:\Program Files (x86)\ArcGIS\...

    NBU10.3.0.1介质

    vxupdate_nb_10.3.0.1_windows_x64.sja vxupdate_nb_10.3.0.1_suse_x64.sja vxupdate_nb_10.3.0.1_redhat_x64.sja NetBackup_10.3.0.1_Win.zip NetBackup_10.3.0.1_LinuxS_x86_64.tar.gz NetBackup_10.3.0.1_LinuxR_...

    GExperts 10.3_1.3.14 for xe 10.3

    《GExperts 10.3_1.3.14 forXE 10.3:提升IDE效率的利器》 GExperts,一个专为XE 10.3开发环境量身定制的强大工具集,旨在显著提升程序员的工作效率,提供丰富的自定义功能,使代码编写、调试和项目管理变得更加...

    orcad10.3免安装版

    **OrCAD 10.3 免安装版详解** OrCAD是一款由Cadence设计系统公司推出的电子设计自动化(EDA)软件,主要用于电路原理图绘制、PCB布局布线以及电路仿真等任务。OrCAD 10.3是该系列的一个重要版本,提供了许多功能...

    RadStudioKeygen 10.3 Rio 4364 破解

    RadStudioKeygen 10.3 Rio 4364 破解

    10.3 Developer Disk Image 调试包

    "10.3 Developer Disk Image"是苹果为iOS 10.3版本提供的一种调试工具,它包含了开发人员进行应用程序调试所需的所有组件。这个调试包主要是为了帮助开发者在iOS 10.3环境下进行应用程序的开发、测试和优化。 首先...

    ORCAD-10.3-Licence生成程序.rar

    《ORCAD 10.3 Licence生成程序详解》 在电子设计自动化(EDA)领域,ORCAD是一款广泛应用的电路设计软件,尤其在PCB设计方面具有显著地位。ORCAD 10.3作为其一个重要版本,深受工程师们的青睐。然而,软件的使用离...

    Orcad10.3绿色版____使用说明 -- 雨花石

    ### Orcad 10.3绿色版使用说明详解 #### 一、Orcad 10.3绿色版概述 Orcad是一款广泛应用于电子设计自动化领域的软件工具,由Cadence公司开发。Orcad 10.3绿色版作为一款特定版本,在特定场景下具有独特的优势。...

    arcengine10.3破解

    根据提供的文件信息,本文将详细解析与“arcengine10.3破解”相关的知识点,包括ArcGIS Engine的基本概念、安装步骤、以及版本升级等方面的信息。 ### ArcGIS Engine 概述 ArcGIS Engine 是Esri公司推出的一款用于...

    arcgis server 10.3 许可文件

    arcgis server 10.3 许可文件,拿出来和大家一起分享。

    iOS DeviceSupport 10.3

    `iOS DeviceSupport 10.3` 提供了针对iOS 10.3版本的设备模拟环境,这对于那些需要回溯测试或者没有实际iOS 10.3设备的开发者来说尤为重要。Xcode是Apple官方的集成开发环境(IDE),它包含了开发iOS应用所需的全部...

    ArcGIS Server 10.3破解文件

    ArcGIS Server 10.3破解文件,解决10.2版本的破解文件在10.3上无法进行破解的问题

    ArcGIS Server 10.3.ecp

    ArcGIS Server 10.3.ecp

    编程工具\XE10.3破解\XE10.3注册机.rar

    Delphi10.3注册机,完美激活最新版的delphi10.3(XE10.3)哦,就不多讲了,大家用着吧,爽。不过Delphi好象没落了,现在下个什么都要钱,要积分,本来资料就少。唉,我也是没办法,想下个东本没积分,传个东东来找点...

    Raize for delphi10.3

    《 Raize Components 6.2.3:在 Delphi 10.3 中的VCL标准化组件探索》 Raize Components 是一套强大的VCL(Visual Component Library)组件集,专为 Delphi 开发者设计,提供了丰富的用户界面控件和功能。在 Delphi...

    iOS_10.3 (14E269)_DeveloperDiskImage for 10.3

    标题中的“iOS_10.3 (14E269)_DeveloperDiskImage for 10.3”指的是苹果公司为iOS开发人员发布的特定版本的iOS操作系统开发者镜像。这个镜像是为了支持Xcode开发环境,让开发者能够在模拟器或实际设备上测试和调试...

    K3 10.3破解文件

    K3 10.3破解轻松实现,无须注册无病毒,安全使用

    DELPHI XE 10.3注册机

    亲测可破解delphi xe 10.3,安装前最好是把以前版本删除干净,包括注册表

    delphi10.3控件安装方法

    ### Delphi 10.3 控件安装方法详解 #### 一、前言 Delphi 是一款非常强大的集成开发环境(IDE),广泛应用于Windows应用程序的快速开发。Delphi 10.3(也称为 Delphi 10.3 Rio)是Embarcadero公司推出的一个版本,...

    ArcGIS Engine 10.3下载

    ### ArcGIS Engine 10.3相关知识点 #### 一、ArcGIS Engine简介 ArcGIS Engine 是由Esri公司开发的一款强大的地理信息系统(GIS)组件技术。它为开发者提供了创建和集成GIS应用的能力,使用户能够在桌面应用程序中...

Global site tag (gtag.js) - Google Analytics