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 827the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8651. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 9171. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8871.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7611. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 9061. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 7101. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 11141. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 755in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 784in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 673again, we will start from TD ... -
10.1 updating users.
2011-10-14 18:30 7161. git checkout -b updating-use ... -
9.4 sign out
2011-10-13 15:21 743whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 7571. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 735There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 667start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 648a 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 730in integration test, you can te ...
相关推荐
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 ...
RHEL_8.2 ISO
Ehlib 8.2 Build 8.2.013 DCU Version
《Ghost 8.2:系统恢复与备份的实用工具》 Ghost 8.2 是一款在IT行业内广泛应用的系统恢复和备份软件,尤其适合个人用户和小型企业进行数据保护。这款软件以其简单易用的“一键还原”功能,使得复杂的系统备份和...
PCS7 V8.2 授权是西门子过程控制系统(Process Control System 7)的一个重要环节,用于确保软件的合法性和正确运行。PCS7是一款综合的过程自动化系统,广泛应用于化工、石油天然气、制药等行业的生产过程控制。V8.2...
### SIMATIC NET V8.2 知识点详解 #### 一、SIMATIC NET V8.2 概述 SIMATIC NET V8.2 是西门子推出的一款用于工业自动化网络配置与通信软件包,它提供了多种通信协议的支持,如PROFIBUS、PROFINET等,帮助用户实现...
天正结构8.2破解补丁
1. **安装与启动**:下载并解压"NetBeans 8.2.zip"后,用户可以找到安装程序,双击运行进行安装。安装过程中,用户可以选择自定义安装路径和要安装的组件,如Java SE、Java EE、PHP或Web开发相关的工具。 2. **集成...
DevExpress8.2 破解文件
LAB VIEW 8.2破解机,我在实验室取的。绝对好用!
在给定的压缩包文件中,我们有两个版本的Gradle 8.2:一个是全量版(`gradle-8.2-all.zip`),另一个是精简版(`gradle-8.2-bin.zip`)。了解这两个版本的区别以及如何设置和使用它们对于有效地管理项目构建至关重要...
Ubuntu 配置 Netbeans 8.2 Ubuntu 是一个流行的 Linux 发行版,而 Netbeans 是一个功能强大的集成开发环境(IDE)。在本文中,我们将指导您如何在 Ubuntu 20.04 上配置 Netbeans 8.2。 标题解释 Ubuntu 配置 ...
LVGL8.2版本是这个库的一个重要里程碑,它提供了一系列增强的功能和改进,使得在有限资源的硬件上构建用户友好的图形界面成为可能。以下是关于LVGL8.2版本源码的详细知识点: 1. **LVGL框架**: LVGL是一个层次化...
CANoe 8.2 基础操作知识点 CANoe 8.2 是一款功能强大且广泛应用于汽车电子、工业自动化、医疗器械等领域的测试和开发工具。本节将详细介绍 CANoe 8.2 的基础操作,包括工程配置、加载 dbc 文件、数据分析和模拟网络...
windows10 ,php8.2, redis 扩展
HyperlynxV8.2 Pojie程序 Win7成功安装
PCS 7 v8.2是其较新的版本,它具备强大的数据处理能力,支持复杂的自动化任务,并提供用户友好的操作界面。本文将详细介绍PCS 7 v8.2在Windows 10 1507英文版操作系统上的安装步骤,确保读者能够顺利进行安装与配置...
本文提供了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 ...
Navicat 8.2 MySQL是一款强大的MySQL数据库管理和开发工具,专为MySQL数据库设计,集成了多种实用功能,包括数据管理、数据传输、备份、数据同步、报表创建以及计划任务等,极大地提升了数据库管理和开发的效率。...
解压zip包,rpm进行更新以及安装openssl8.2 使用yum安装依赖 zip包: openssh-8.2p1-1.el7.x86_64.rpm openssh-clients-8.2p1-1.el7.x86_64.rpm openssh-server-8.2p1-1.el7.x86_64.rpm