2016-03-03 139 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