Ca75fbd03a327f616e84538423cc05ba

Rails101 nested routes問題求解

2012-07-11 18:02:33 +0800dpanabenRuby on Rails 節點 中發起
最後由 elct96202012-07-13 22:55:33 +0800回應 , 233次閱讀

目前在練習Rails101裏面的nested routes,原本已經可以正常運作,但是後來把資料清空重新要建立資料時遇到問題
要新增一筆board資料會出現下列路徑
http://forum-demo.dev/boards/new
但是卻遇到下面的error
No route matches [POST] "/boards/new"
跑一下rake routes
new_board GET /boards/new(.:format) boards#new
看起來GET 跟 POST好像有問題,請問這是出了什麼問題嗎?

我後來在routes.rb加了match ':controller(/:action(/:id))(.:format)',但是只有board可以新增,到了post又出問題了。

截至 2012-07-13 22:55:33 +0800,共收到 6 條回應
Ca75fbd03a327f616e84538423cc05ba
dpanaben 1樓, 於2012-07-11 18:18:28 +0800回應

我試著在routes.rb裏面加了下面兩行,看起來是沒問題了。

match ':controller(/:action(/:id))(.:format)'
match ':controller/:id/:controller/(:action/(:id))(.:format)'

888339de9e7a88688b6acb30d33e66cd
elct9620 2樓, 於2012-07-11 18:34:56 +0800回應

不是:

resources :boards do
  resources :posts
end

這樣嗎?

Ca75fbd03a327f616e84538423cc05ba
dpanaben 3樓, 於2012-07-11 23:10:01 +0800回應

只有設定 nested resources 在 new action時就會出現我原來的問題。我是加了兩行match之後才解決。不知道有沒有其他方法,或是nested resources 的地方要改?

888339de9e7a88688b6acb30d33e66cd
elct9620 4樓, 於2012-07-12 10:02:02 +0800回應

會不會是你 Form 設定錯誤,我的印象中 /boards/new 是 GET 而 /boards 有 POST 可以接收資料(回想中)

http://guides.rubyonrails.org/routing.html

嗯,接收 POST 資料不是 /boards/new 而是 /boards
(所以Router應該沒錯,是你表單 submit 的地方錯了)

Ca75fbd03a327f616e84538423cc05ba
dpanaben 5樓, 於2012-07-13 16:19:53 +0800回應

後來不知不覺就好了xd

888339de9e7a88688b6acb30d33e66cd
elct9620 6樓, 於2012-07-13 22:55:33 +0800回應

這也太神奇了 XDD

需要 登入 後方可回應,如果你還沒有帳號按這裡 註冊