Rails 嵌套资源不工作
Rails nested resources not working
我试图按照本教程将 Stripe 添加到我的 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
是不是因为charges嵌套在listings资源里面,而listings也嵌套在另外一个?这很奇怪,因为如果我将代码改回 none 嵌套资源,它完全可以正常工作。
= 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]
如果你想这样做,你的路径是 people_listing_charge_path(@people, @listing, @charge)
。或者 person_listing_charge_path(@person, @listing, @charge)
取决于 Rails 助手的工作方式。如您所见,处理起来非常麻烦。
如果您选中 Rails guide on nested route,它会建议一些处理嵌套的方法。我强烈建议您遵循这些建议。
你可能应该这样做(如果你的 people 单数是 person)
person_listing_charge_path(@person, @listing, @charge)
要了解您的路线,您可以rake routes
仅供参考:根据 ruby on rails guides
Resources should never be nested more than 1 level deep.
我试图按照本教程将 Stripe 添加到我的 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
是不是因为charges嵌套在listings资源里面,而listings也嵌套在另外一个?这很奇怪,因为如果我将代码改回 none 嵌套资源,它完全可以正常工作。
= 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]
如果你想这样做,你的路径是 people_listing_charge_path(@people, @listing, @charge)
。或者 person_listing_charge_path(@person, @listing, @charge)
取决于 Rails 助手的工作方式。如您所见,处理起来非常麻烦。
如果您选中 Rails guide on nested route,它会建议一些处理嵌套的方法。我强烈建议您遵循这些建议。
你可能应该这样做(如果你的 people 单数是 person)
person_listing_charge_path(@person, @listing, @charge)
要了解您的路线,您可以rake routes
仅供参考:根据 ruby on rails guides
Resources should never be nested more than 1 level deep.