2016-03-03 113 views
1

我試圖條紋添加到下面這個教程我的Rails應用程序,但無法弄清楚什麼地方出了錯:Rails的嵌套的資源不工作

Assigning Charges To Resources With Stripe Checkout

下面是我的代碼,

routes.rb中

resources :people, :path => "" do 
    member do 
     put :activate 
     put :deactivate 
    end 
    resources :listings do 
     member do 
     put :close 
     put :move_to_top 
     put :show_in_updates_email 
     end 
     resources :charges 
    end 

和the.haml

= form_tag listings_charges_path(@listings) do 

,但有錯誤象下面這樣:

undefined method `listings_charges_path' for #<#<Class:0x007f0690ea1788>:0x007f06b1864b88> 

     = form_tag listings_charges_path(@listings) do 

是因爲收費嵌套在上市資源和上市也嵌套在另一個呢?這很奇怪,因爲如果我將代碼更改爲無嵌套資源,它完全正常工作。

= form_tag charges_path do 

任何幫助真的很感激。

謝謝!

這個工作,但也有一些是新的:

  = form_tag person_listing_charges_path(@person, @listing, @charges) do 

新的錯誤:

No route matches missing required keys:{:action=>"index", :controller=>"charges", :id=>"111-abc", :listing_id=>nil, :locale=>nil, :person_id=>#<Listing id: 111, ..........} [:listing_id] 
+0

您還需要在路徑方法中傳遞'@ charge'對象。 –

回答

0

你的路徑是people_listing_charge_path(@people, @listing, @charge),如果你想這樣做。或者,也許取決於Rails助手的工作方式。正如你所看到的,處理起來非常麻煩。

如果您檢查Rails guide on nested route,它提供了一些處理嵌套的方法。我強烈建議你遵循這些建議。

+0

謝謝,@Harfangk! **這個工作,但也有一些是新的:** =的form_tag person_listing_charges_path(@person,@listing,@charges)做 **新的錯誤:** 沒有路由匹配缺少必需的鍵:{ :action =>「index」,:controller =>「charges」,:id =>「111-abc」,::listing_id => nil,:locale => nil,:person_id =>#<上市ID:111,。 .........} [:listing_id] –

0

你或許應該這樣做(如果你的人奇異的是人)

person_listing_charge_path(@person, @listing, @charge) 

要知道你的路線,你可以做rake routes

FYI:根據ruby on rails guides

Resources should never be nested more than 1 level deep.

+0

謝謝,@XavM! **這個工作,但也有一些是新的:** =的form_tag person_listing_charges_path(@person,@listing,@charges)做 **新的錯誤:** 沒有路由匹配缺少必需的鍵:{ :action =>「index」,:controller =>「charges」,:id =>「111-abc」,::listing_id => nil,:locale => nil,:person_id =>#<上市ID:111,。 .........} [:listing_id] –

+0

傳遞參數似乎有錯誤: listing_id是nil - >你確定'@ listing'不是空的嗎? person_id包含一個列表對象 - >你確定'@ person'包含People對象嗎?你可以發佈你的路線person_listing_charge_path(通過耙路線訪問)。 – XavM