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

8.2 sign up failure

 
阅读更多

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]
 

 

分享到:
评论

相关推荐

    Ehlib 8.2 Build 8.2.013 DCU Version

    Ehlib 8.2 Build 8.2.013 DCU Version

    ghost8.2系统恢复及备份

    《Ghost 8.2:系统恢复与备份的实用工具》 Ghost 8.2 是一款在IT行业内广泛应用的系统恢复和备份软件,尤其适合个人用户和小型企业进行数据保护。这款软件以其简单易用的“一键还原”功能,使得复杂的系统备份和...

    RHEL_8.2 ISO

    RHEL_8.2 ISO

    DevExpress控件forC#-8.2

    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 授权

    PCS7 V8.2 授权是西门子过程控制系统(Process Control System 7)的一个重要环节,用于确保软件的合法性和正确运行。PCS7是一款综合的过程自动化系统,广泛应用于化工、石油天然气、制药等行业的生产过程控制。V8.2...

    DevExpress8.2 破解文件-免费

    DevExpress8.2 破解文件

    SIMATIC NET V8.2.txt

    ### SIMATIC NET V8.2 知识点详解 #### 一、SIMATIC NET V8.2 概述 SIMATIC NET V8.2 是西门子推出的一款用于工业自动化网络配置与通信软件包,它提供了多种通信协议的支持,如PROFIBUS、PROFINET等,帮助用户实现...

    gradle8.2版本(bin+all)

    在给定的压缩包文件中,我们有两个版本的Gradle 8.2:一个是全量版(`gradle-8.2-all.zip`),另一个是精简版(`gradle-8.2-bin.zip`)。了解这两个版本的区别以及如何设置和使用它们对于有效地管理项目构建至关重要...

    hyperlynx 8.2 破解程序

    HyperlynxV8.2 Pojie程序 Win7成功安装

    pcs7v8.2的安装步骤

    PCS 7 v8.2是其较新的版本,它具备强大的数据处理能力,支持复杂的自动化任务,并提供用户友好的操作界面。本文将详细介绍PCS 7 v8.2在Windows 10 1507英文版操作系统上的安装步骤,确保读者能够顺利进行安装与配置...

    LVGL8.2版本源码

    LVGL8.2版本是这个库的一个重要里程碑,它提供了一系列增强的功能和改进,使得在有限资源的硬件上构建用户友好的图形界面成为可能。以下是关于LVGL8.2版本源码的详细知识点: 1. **LVGL框架**: LVGL是一个层次化...

    netbeans8.2.zip

    1. **安装与启动**:下载并解压"NetBeans 8.2.zip"后,用户可以找到安装程序,双击运行进行安装。安装过程中,用户可以选择自定义安装路径和要安装的组件,如Java SE、Java EE、PHP或Web开发相关的工具。 2. **集成...

    kettle 8.2相关jar

    本文提供了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配置Netbeans8.2

    Ubuntu 配置 Netbeans 8.2 Ubuntu 是一个流行的 Linux 发行版,而 Netbeans 是一个功能强大的集成开发环境(IDE)。在本文中,我们将指导您如何在 Ubuntu 20.04 上配置 Netbeans 8.2。 标题解释 Ubuntu 配置 ...

    QC8.2和QC9.0相关资料大全

    (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网盘下载地址.txt

    kettle8.2下载地址,真实有效!!!

    Jason 8.2 .dat

    .dat file for jason 8.2

    DevComponents.DotNetBar2 8.2

    DevComponents.DotNetBar2 8.2 是一款强大的Windows Forms控件库,专为.NET Framework开发者设计,用于提升应用程序的用户界面(UI)体验。这个版本8.2提供了丰富的UI元素和工具,使得开发者能够轻松创建出专业、...

    安装pcs7v8.2.doc

    PCS7 v8.2是该系统的其中一个版本,提供了更加强大、灵活和集成的功能,以满足现代工业生产的需求。以下是对安装PCS7 v8.2的详细步骤的解释: 1. **系统需求**:首先,确保你的计算机运行的是Windows 7 Ultimate ...

    WinRunner8.2安装软件12

    WinRunner8.2安装软件12

Global site tag (gtag.js) - Google Analytics