chapter 8.1
1. let's first make a branch for the sign up chapter:
git checkout -b signing-up
2. also reset the database:
rake db:reset
3. we already got two basic test for new action in users_controller_spec.rb test
describe "GET 'new'" do it "should be successful" do get :new response.should be_success end it "should have the right title" do get :new response.should have_selector("title", :content => "Sign up") end end
next, we will make a for in the new.htm.erb file
we will use form_for helper method.
(in prior rails, it use <% form_for %>
but in rails 3, it use <%= form_for %)
4.
<h1>Sign up</h1>
<%= form_for(@user) do |f| %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :email %><br /> <%= f.text_field :email %> </div> <div class="field"> <%= f.label :password %><br /> <%= f.password_field :password %> </div> <div class="field"> <%= f.label :password_confirmation, "Confirmation" %><br /> <%= f.password_field :password_confirmation %> </div> <div class="actions"> <%= f.submit "Sign up" %> </div> <% end %>
a. the "do" indicates that form_for takes a block, which has one var, which we call f for form.
inside the form_for helper, f is an object that represents a form.
b. f.label
f.text_field
f.password_field
c. the created html is:
<div class="field"> <label for="user_password">Password</label><br /> <input id="user_password" name="user[password]" size="30" type="password" /> </div>
here, the key is the special name attr, "user[password]"
The name values allow rails to construct an init hash(via the params var.), this hash will be used to create user.
d. the second important element is the form tag itself.
rails create the form tag using the @user object.
because every ruby object knows its own class, rails figures out that @user is of class User,
moreover, since @user is a new user, rails knows to construct a form with post method.
<form action="/users" class="new_user" id="new_user" method="post">
here, the class and id are not very useful, what matters is the action and method attrs.
action tell rails the objective url, and post tell rails this is a post action, want to create a new user.
so the objective action is "create" in users controller.
e. then we can see the "authenticity token" field,
<input name="authenticity_token" type="hidden" value="rB82sI7Qw5J9J1UMILG/VQL411vH5putR+JwlxLScMQ=" />
here rails uses a special unique value to avoid a particular kind of cross-site scription attack called a forgery.
happily, rails takes care of it for you, and the input tag is hidden.
f. look at the password confirmation part, the label text is different from the field name, so
f.label take a second param, "Confirmation"
发表评论
-
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 642start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 636a 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 ...
相关推荐
Win8.1Up1+Office2013Sp1+VS2013Up4 19of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 15of 20
533312664664446Dynamic Island_8.1_sign.apk
Win8.1Up1+Office2013Sp1+VS2013Up4 8of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 4of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 7of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 1of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 6of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 2of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 18of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 3of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 13of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 5of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 10of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 17of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 9of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 14of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 20of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 12of 20
Win8.1Up1+Office2013Sp1+VS2013Up4 11of 20