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是行内元素(也译作内嵌元素)。
块元素和行内元素也不是一成不变的,通过定义CSS的display属性值可以互相转化,如:
测试<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 @usersrails 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.
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 816the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8541. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9101. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8761.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7481. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8941. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6981. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11031. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 736in this chapter, we will add de ... -
10.2 protect pages.
2011-10-15 15:11 657again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7061. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 732whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7421. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 722There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 657start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 644a session is a semi-permanent c ... -
what test framework should you use?
2011-10-11 16:56 0for integration test, i have no ... -
what test framework should you use?
2011-10-11 16:56 0<p>for integration test, ... -
8.4 rspec integration tests
2011-10-11 16:53 714in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 776Chapter 8.3 this part, we will ...
相关推荐
Arcgis 10.3破解文件,使用方法: 1、自行安装lm,关闭服务,拷贝破解文件①② 2、自行安装desktop,拷贝破解文件③,启动lm服务。 破解覆盖文件所在路径: ①service.txt--->C:\Program Files (x86)\ArcGIS\...
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 forXE 10.3:提升IDE效率的利器》 GExperts,一个专为XE 10.3开发环境量身定制的强大工具集,旨在显著提升程序员的工作效率,提供丰富的自定义功能,使代码编写、调试和项目管理变得更加...
RadStudioKeygen 10.3 Rio 4364 破解
"10.3 Developer Disk Image"是苹果为iOS 10.3版本提供的一种调试工具,它包含了开发人员进行应用程序调试所需的所有组件。这个调试包主要是为了帮助开发者在iOS 10.3环境下进行应用程序的开发、测试和优化。 首先...
《ORCAD 10.3 Licence生成程序详解》 在电子设计自动化(EDA)领域,ORCAD是一款广泛应用的电路设计软件,尤其在PCB设计方面具有显著地位。ORCAD 10.3作为其一个重要版本,深受工程师们的青睐。然而,软件的使用离...
根据提供的文件信息,本文将详细解析与“arcengine10.3破解”相关的知识点,包括ArcGIS Engine的基本概念、安装步骤、以及版本升级等方面的信息。 ### ArcGIS Engine 概述 ArcGIS Engine 是Esri公司推出的一款用于...
`iOS DeviceSupport 10.3` 提供了针对iOS 10.3版本的设备模拟环境,这对于那些需要回溯测试或者没有实际iOS 10.3设备的开发者来说尤为重要。Xcode是Apple官方的集成开发环境(IDE),它包含了开发iOS应用所需的全部...
### Orcad 10.3绿色版使用说明详解 #### 一、Orcad 10.3绿色版概述 Orcad是一款广泛应用于电子设计自动化领域的软件工具,由Cadence公司开发。Orcad 10.3绿色版作为一款特定版本,在特定场景下具有独特的优势。...
ArcGIS Server 10.3.ecp
arcgis server 10.3 许可文件,拿出来和大家一起分享。
ArcGIS Server 10.3破解文件,解决10.2版本的破解文件在10.3上无法进行破解的问题
《 Raize Components 6.2.3:在 Delphi 10.3 中的VCL标准化组件探索》 Raize Components 是一套强大的VCL(Visual Component Library)组件集,专为 Delphi 开发者设计,提供了丰富的用户界面控件和功能。在 Delphi...
### ArcGIS Engine 10.3相关知识点 #### 一、ArcGIS Engine简介 ArcGIS Engine 是由Esri公司开发的一款强大的地理信息系统(GIS)组件技术。它为开发者提供了创建和集成GIS应用的能力,使用户能够在桌面应用程序中...
标题中的“iOS_10.3 (14E269)_DeveloperDiskImage for 10.3”指的是苹果公司为iOS开发人员发布的特定版本的iOS操作系统开发者镜像。这个镜像是为了支持Xcode开发环境,让开发者能够在模拟器或实际设备上测试和调试...
K3 10.3破解轻松实现,无须注册无病毒,安全使用
亲测可破解delphi xe 10.3,安装前最好是把以前版本删除干净,包括注册表
### Delphi 10.3 控件安装方法详解 #### 一、前言 Delphi 是一款非常强大的集成开发环境(IDE),广泛应用于Windows应用程序的快速开发。Delphi 10.3(也称为 Delphi 10.3 Rio)是Embarcadero公司推出的一个版本,...
当标题提到“iOS10.3 xcode真机支持包”,这通常指的是一个包含了特定iOS版本(本例中是10.3)的设备支持文件的集合,这些文件允许开发者在Xcode上对运行iOS 10.3系统的实际设备进行应用的调试和测试。 首先,我们...
《Orcad-10.3 免安装精简版:高效便捷的电路设计工具》 Orcad是一款由Cadence Design Systems公司推出的强大的电子设计自动化(EDA)软件,广泛应用于电路设计、仿真和PCB布局布线等领域。Orcad-10.3 免安装精简版...