2009-12-05 73 views

回答

4

form_for功能將在發送表單的最佳猜測:

就拿下面的代碼:

<% form_for @user do |form| %> 
  1. 如果@user是一個新的記錄,它會發送一個POST請求到/users
  2. 如果@user是一個現有記錄,它將發送一個PUT請求到/users/12 (如果12 = @ user.id)
0

處理更新的標準Rails方法是創建一個編輯模板(您的edit.html.erb),該模板生成一個由用戶填充並作爲HTTP PUT請求提交的表單。您的Rails應用程序應該爲正在更新的資源創建一個模型,並使用一個具有「更新」操作的控制器來接受表單參數。

如果要查看PUT請求中正在發送的數據,可以使用Firebug

1

你可以看到把方法上的編輯,當您運行

rake routes 


new_account GET /account/new(.:format) {:action=>"new", :controller=>"accounts"} 
edit_account GET /account/edit(.:format) {:action=>"edit", :controller=>"accounts"} 
account  GET /account(.:format)  {:action=>"show", :controller=>"accounts"} 
      PUT /account(.:format)  {:action=>"update", :controller=>"accounts"} //this is your method needed 
      DELETE /account(.:format)  {:action=>"destroy", :controller=>"accounts"} 
      POST /account(.:format)  {:action=>"create", :controller=>"accounts"} 

<% form_for @user do |form| %> can be <% form_for :user, :url => user_url(:user), :method => :put do |form| %>