1. git checkout -b updating-users
2. in this chapter, we will make you can update user profile.
a. we will use a "edit" action to render a view to edit user.
b. we will use "update" action and a "PUT" request to update user profile
c. we need to make sure only current user can update their information. this need a "before_filter"
3. edit form, we will start from TDD again!!
describe UserController do describe "get 'edit'" do before(:each) do @user = Factory(:user) test_sign_in(@user) end it "should be success" do get :edit, :id => @user response.should be_success end it "should have the right title" do get :edit, :id => @user response.should have_selector("title", :content => "Edit user") end it "should have the link to gravatar" do get :edit, :id => @user gravatar_url = "http://gravatar.com/emails" response.should have_selector("a", :href => gravatar_url, :content => "change") end end end
4. now it is time to write the view code:
<h1>Edit User</h1> <%= form_for @user do |f| %> <%= render "shared/error_messages", :object => f.object %> <div class="field"> <%= f.label :name %> <br /> <%= f.text_field :name %> <br /> <%= f.label :email %> <br /> <%= f.text_field :email %> <br /> <%= f.label :password %> <br /> <%= f.password_field :password %> <br /> <%= f.label :password_confirmation, "Confirmation" %><br /> <%= f.password_field :password_confirmation %> <br /> </div> <div class="action"> <%= f.submit "Update"%> </div> <% end %> <div> <%= gravatar_for @user %> <a href="http://gravatar.com/emails">Change</a> </div>
you can find we passed a object param when rendering partial .
this is common because a real partial should not rely on the fact that there is a @user object.
this is especially useful when composing a form,
<%= render 'shared/error_messages', :object => f.object %>
this create a var called object in the partial,
now we need to re-write the _error_messages partial:
<% if object.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(object.errors.count, "error") %> prohibited this <%= object.class.to_s.underscore.humanize.downcase %> from being saved:</h2> <p>There were problems with the following fields:</p> <ul> <% object.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %>
we can learn two helper method here:
"ActiveRecord".underscore ======> active_record (lower case and add _ )
"active_record".humanize =======> Active record (capitalize, and replace _ with space.)
5. next, let's look at the html gened by the form:
<form action="/users/1" class="edit_user" id="edit_user_1" method="post"><input name="_method" type="hidden" value="put" />. . . </form>
note this hidden line:
since web browser can't natively send "PUT" request, rails fake it with a post request, and a hidden input field.
6. there is another magic that you may wondering,
we use the same code for edit form and new form, but why the html generated are different?
for new action, rails use a post method, and for edit action, rails use a put method.
the answer is simple and trikey, rails will run
@user.new_record?
to judge if this record is a new one, or already exist in database.
so rails will know to use a put request or post request, clever?? cool!
7. next is the test for update success and update failure.
describe "PUT 'update'" do before(:each) do @user = Factory(:user) test_sign_in(@user) end describe "update failure" do before(:each) do @attr = {:name => "", :email => "", :password => "", :password_confirmation => "" } end it "should render the edit page" do put :update, :id => @user, :user => @attr response.should render_template('edit') end it "should have the right title" do put :update, :id => @user, :user => @attr response.should have_selector("title", :content => "Edit user") end end describe "update success" do before(:each) do @attr = { :name => "New Name", :email => "user@example.org", :password => "barbaz", :password_confirmation => "barbaz" } end it "should redirect to user show page" do put :update, :id => @user, :user => @attr response.should redirect_to user_path(@user) end it "should change user's attrs" do put :update, :id => @user, :user => @attr @user.reload @user.name.should == @attr[:name] @user.email.should == @attr[:email] end it "should have a flash message" do put :update, :id => @user, :user => @attr flash[:success].should =~ /updated/i end end end
one thing to note:
@user.reload ========> this will reload the @user content from database.
8. next, we will implement the update method in the controller:
def update @user = User.find_by_id(params[:id]) if @user.update_attributes(params[:user]) flash[:success] = "Profile updated" redirect_to @user else @title = "Edit user" render 'edit' end end
发表评论
-
12.3.3 scaling issue of the status feed
2011-10-30 17:54 738the problem of the implementati ... -
12.3 the status feed
2011-10-30 15:34 8051. we need to get all the micro ... -
12.2 a working follow button with Ajax
2011-10-29 18:10 8771. in the last chapter, in the ... -
12.2 a web interface for following and followers.
2011-10-28 22:14 8351.before we do the UI, we need ... -
12. following user, 12.1 relationship model
2011-10-18 14:29 7021. we need to use a relationshi ... -
11.3 manipulating microposts.
2011-10-17 15:31 8561. since all micropost actions ... -
11.2 show microposts.
2011-10-17 12:01 6481. add test to test the new use ... -
11.1 user micropost -- a micropost model.
2011-10-17 10:43 10631. we will first generate a mic ... -
10.4 destroying users.
2011-10-16 15:47 688in this chapter, we will add de ... -
10.3 showing users list
2011-10-15 20:41 731in this chapter, we will do use ... -
10.2 protect pages.
2011-10-15 15:11 603again, we will start from TD ... -
9.4 sign out
2011-10-13 15:21 682whew!!!, last chapter is a long ... -
9.3 sign in success.
2011-10-12 15:39 6811. we will first finish the cre ... -
9.1 about flash.now[:error] vs flash[:error]
2011-10-12 15:37 670There’s a subtle difference ... -
9.2 sign in failure
2011-10-12 12:19 605start from TDD!!! 1. requir ... -
9.1 sessions
2011-10-12 10:00 612a 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 675in integration test, you can te ... -
8.3 sign up success
2011-10-11 14:39 732Chapter 8.3 this part, we will ...
相关推荐
LeetCode of algorithms with golang solution(updating).
删除mysql的日志文件,重启mysql发现错误:Starting MySQL.Manager of pid-file quit without updating file.[FAILED] 网上有不少这个原因的解释,但是都不是我想说的。我要说的原因其实很白痴:data/mysql-bin....
今天小编就为大家分享一篇解决pycharm启动后总是不停的updating indices...indexing的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
版本更新,补丁,unity pc平台
Chapter 1 Introduction to the SQL Procedure ...Chapter 4 Creating and Updating Tables and Views Chapter 5 Programming with the SQL Procedure Chapter 6 Practical Problem-Solving with PROC SQL
paper :artist_palette: paper 是一个简洁,没有过多冗余视觉元素和功能的 hexo 主题,其设计风格的灵感来源于:newspaper: 报纸等纸质读物。而且主题以一种:light_bulb:极其巧妙的方式实现了当下(2019)流行的:last_...
cmpr 软件的更新软件,一般浏览器网页下载不下来。 cmpr_updating
idea的文件一直在不停闪烁,并不停updating and index.doc
Unity的更新系统!
PART II Querying and Updating Data . . . . . . . . . . . . . . . . 71 CHAPTER 5 SELECT Statement: Common Elements . . . . . . . . . . . . 73 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . ...
14.4 Updating......275 14.5 Closing Thoughts..... . . 276 V Appendixes 277 A Java vs. the Android Language and APIs 278 A.1 Language Subset..... . . 278 A.2 Standard Library Subset.... . . 280 A...
APIS (keep updating...) 权限相关 [Demo] ActivityExt [Demo] AppExt AesExt [Demo] CommonExt Constants DrawableExt FileExt HashExt [Demo] IntentExt [Demo] KtxManager [Demo] KtxSpan [Demo] ...
如下所示: ...以上这篇解决pycharm启动后总是不停的updating indices…indexing的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文
10.1 Basic Example of using subplot2grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 10.2 GridSpec and SubplotSpec. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
chromium的depot_tools,windows版本,如果你被墙挡住了,可能需要它。这是我用国外的云服务器下载回来的,更新日期2016年10月28日。 需要macos版本的请搜索我的另一个资源chromium depot_tools for macos。
Everything search engine Small installation file Clean and simple user interface Quick file indexing Quick searching Minimal resource usage Share files with others easily Real-time updating More...
crates.io-index:crates.io的注册表索引
Using this Object-oriented application users can perform management of employees such as adding new employee details editing/updating their details removing them from the system or viewing all ...
最好用的版本:docker-engine_1.12.6-0-ubuntu-trusty_amd64.deb