chapter 8.2
for this section, we will do this:
if a signup form has invalid submission, the app will re-render the signup page, show the error details.
1. we will do TDD again!!!
resources :users
this line in routes.rb prepare many REST urls for us.
for example, a POST request to /users is handled by the create action.
2. the create action is to use the form submission to make a new user object using User.new, try to save this user object, and then render the signup page for resubmission.
so we will write test to test this work flow, then write create action to Users controller to make it pass.
let's see the test code first:
require 'spec_helper' describe UsersController do render_views . . . describe "POST 'create'" do describe "failure" do before(:each) do @attr = { :name => "", :email => "", :password => "", :password_confirmation => "" } end it "should not create a user" do lambda do post :create, :user => @attr end.should_not change(User, :count) end it "should have the right title" do post :create, :user => @attr response.should have_selector("title", :content => "Sign up") end it "should render the 'new' page" do post :create, :user => @attr response.should render_template('new') end end end end
a. response.should have_selector()
b. response.should render_template('new')
c. then let's learn two new tricks in the first example.
it "should not create a user" do lambda do post :create, :user => @attr end.should_not change(User, :count) end
this is to verify the failed create action doesn't create a user in the database.
---> should_not change(User, :count)
this will compare the User.count value before and after the excution of the former part of code.
----> the lambda is a Ruby construct, it will take a block, wrap the codes in the block into a package.
3. next, we will implement the create function.
def create @user = User.new(params[:user]) if @user.save # Handle a successful save. else @title = "Sign up" render 'new' end end
when invoking the create action, our app will receive a params hash
params(:user) is
{:name => "", :email => "", .......}
params(:action)
params(:authenticity_token)
params(:controller)
the magic come from the name attr in the element of html form.
<input id="user_email" name="user[email]" size="30" type="text" />
note, if the submit fails, in the re-rendered page, some fields are pre-filled.
the reason is that,
form_for auto fills in the fields with the attr of @user object.
so
<input id="user_name" name="user[name]" size="30" type="text" value="Foo"/>
4. sign up error messages:
We want to display the error messages.
so we render a partial into the new.html.erb:
<%= render 'shared/error_messages' %>
so the partial lives in views/shared/_error_messages.html.erb
<% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user rom being saved:</h2> <p>There were problems with the following fields:</p> <ul> <%@user.errors.full_messages.each do |msg| %> <li><%= msg%></li> <% end %> </ul> </div> <% end %>
from this part of code, we learned:
User.count---------> return the record count.
array.count -------> return the count of the array.
array.empty?
array.any?
b. we learn the helper method:
pluralize(@user.errors.count, "error")
c. we used id="error_explanation", so in custom.css file, there be an entry of #error_explanation
(#... to specify a css of a id)
and Rails automatically wraps the fields with errors in divs with the css class "field_with_errors"
so you can specify your own css by define
.field_with_errors
in your customed css file.
5. filtering params logging.
before rails 3, the password will be recorded in the log file, without being filtered.
Rails 3 changed to default to filter password.
it is set in config/application.rb
config.filter_parameters += [:password]
if you want to filter other fields, like "secret_code"
you can change this config:
config.filter_parameters += [:password, :secret_code]
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 788the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8421. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8961. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8581.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7291. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8821. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6831. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10871. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 715in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 757in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 634again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 6931. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 718whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7211. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 703There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 643start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 637a 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 699in integration test, you can te ...
相关推荐
Ehlib 8.2 Build 8.2.013 DCU Version
《Ghost 8.2:系统恢复与备份的实用工具》 Ghost 8.2 是一款在IT行业内广泛应用的系统恢复和备份软件,尤其适合个人用户和小型企业进行数据保护。这款软件以其简单易用的“一键还原”功能,使得复杂的系统备份和...
RHEL_8.2 ISO
DevExpress控件forC#-8.2完整版 --------------------------------- DevExpress.BonusSkins.v8.2.dll DevExpress.Charts.v8.2.Core.dll DevExpress.Data.v8.2.Compact.dll DevExpress.Data.v8.2.dll ...
PCS7 V8.2 授权是西门子过程控制系统(Process Control System 7)的一个重要环节,用于确保软件的合法性和正确运行。PCS7是一款综合的过程自动化系统,广泛应用于化工、石油天然气、制药等行业的生产过程控制。V8.2...
DevExpress8.2 破解文件
### SIMATIC NET V8.2 知识点详解 #### 一、SIMATIC NET V8.2 概述 SIMATIC NET V8.2 是西门子推出的一款用于工业自动化网络配置与通信软件包,它提供了多种通信协议的支持,如PROFIBUS、PROFINET等,帮助用户实现...
在给定的压缩包文件中,我们有两个版本的Gradle 8.2:一个是全量版(`gradle-8.2-all.zip`),另一个是精简版(`gradle-8.2-bin.zip`)。了解这两个版本的区别以及如何设置和使用它们对于有效地管理项目构建至关重要...
HyperlynxV8.2 Pojie程序 Win7成功安装
PCS 7 v8.2是其较新的版本,它具备强大的数据处理能力,支持复杂的自动化任务,并提供用户友好的操作界面。本文将详细介绍PCS 7 v8.2在Windows 10 1507英文版操作系统上的安装步骤,确保读者能够顺利进行安装与配置...
LVGL8.2版本是这个库的一个重要里程碑,它提供了一系列增强的功能和改进,使得在有限资源的硬件上构建用户友好的图形界面成为可能。以下是关于LVGL8.2版本源码的详细知识点: 1. **LVGL框架**: LVGL是一个层次化...
1. **安装与启动**:下载并解压"NetBeans 8.2.zip"后,用户可以找到安装程序,双击运行进行安装。安装过程中,用户可以选择自定义安装路径和要安装的组件,如Java SE、Java EE、PHP或Web开发相关的工具。 2. **集成...
本文提供了kettle 8.2相关的5个jar,进行kettle相关插件的开发完成够用了。 如下为本kettle压缩包包含的文件: kettle-core-8.2.0.0-342.jar kettle-dbdialog-8.2.0.0-342.jar kettle-engine-8.2.0.0-342.jar ...
Ubuntu 配置 Netbeans 8.2 Ubuntu 是一个流行的 Linux 发行版,而 Netbeans 是一个功能强大的集成开发环境(IDE)。在本文中,我们将指导您如何在 Ubuntu 20.04 上配置 Netbeans 8.2。 标题解释 Ubuntu 配置 ...
(1)QC8.2中文管理员指南.pdf (2)QC8.2中文教程.pdf (3)QC8.2中文安装指南.pdf (4)QC8.2开放式测试体系架构指南.pdf (5)QC9.0BPT英文指南.pdf (6)QC9.0Site_Admin_API_Reference英文版 (7)QC9.0安装...
kettle8.2下载地址,真实有效!!!
.dat file for jason 8.2
DevComponents.DotNetBar2 8.2 是一款强大的Windows Forms控件库,专为.NET Framework开发者设计,用于提升应用程序的用户界面(UI)体验。这个版本8.2提供了丰富的UI元素和工具,使得开发者能够轻松创建出专业、...
PCS7 v8.2是该系统的其中一个版本,提供了更加强大、灵活和集成的功能,以满足现代工业生产的需求。以下是对安装PCS7 v8.2的详细步骤的解释: 1. **系统需求**:首先,确保你的计算机运行的是Windows 7 Ultimate ...
WinRunner8.2安装软件12