Rails 除了原本幫我們設定好的 RESTful 之外,也可以透過修改 config/routes.rb 來自訂路徑。
假設我們要新增一個 url 專門處理 POST request 的話,則:
match 'test/users/' => 'users#test_post', :via => 'post'
加入新的路徑之後,可以使用 rake routes 來檢查,看是否有出現在可始用的 routes 當中。
再來,我們可以使用 curl 做對網址發 post request。假設現在要發帶兩個變數,分別為 name 和 gender 的 post request,則:(如果使用預設的 routes)
$ curl -X POST -d "name=John&gender=male" http://localhost:3000/user
有的會修改 route table,例如加上 token 做安全認證,那麼:
$ curl -X POST -d "name=John&gender=male" http://localhost:3000/user?auth_token=xxxxxx
假設我們要新增一個 url 專門處理 POST request 的話,則:
match 'test/users/' => 'users#test_post', :via => 'post'
加入新的路徑之後,可以使用 rake routes 來檢查,看是否有出現在可始用的 routes 當中。
再來,我們可以使用 curl 做對網址發 post request。假設現在要發帶兩個變數,分別為 name 和 gender 的 post request,則:(如果使用預設的 routes)
$ curl -X POST -d "name=John&gender=male" http://localhost:3000/user
有的會修改 route table,例如加上 token 做安全認證,那麼:
$ curl -X POST -d "name=John&gender=male" http://localhost:3000/user?auth_token=xxxxxx
留言
張貼留言