我跟踪这个教程,当我想展示我的产品时,我得到了这个错误:
undefined method `product_bids_path'在我的路线文件中:
devise_for :users
resources :products do
resources :auctions, only: [:create] do
resources :bids, only: [:create]
end
end在products/show.html.erb中
=render "auction"
=render "bid"这是我的出价部分:
= form_for [ @product, @product.auction, Bid.new] do |f|在这个form_for助手中,它需要发送product_auction_bids_path url请求,但是需要发送product_bids_path url请求。
如何编写向form_for发送请求的正确的product_auction_bids_path助手?
发布于 2015-12-19 09:29:13
好像是评论解决了它;
为了读者的利益,如果您有一组嵌套的路由(如下所示),则必须确保使用所有依赖对象调用路径:
resources :products do
resources :auctions, only: [:create] do
resources :bids, only: [:create]
end
end
products_auctions_bids_path(@product, @auction, @bid)现在。
关于这一点,有一点是很重要的-- 文档声明,您不应该在超过一个级别的范围内嵌套路由:
资源不应嵌套超过1层深度。
您确实需要确保您能够根据所有三种嵌套资源中的primary key的不那么独特的信息调用路由。
我知道您不是在调用单独的路径(只调用create),但是像您正在做的那样嵌套它仍然是一种糟糕的模式。
有几种补救方法;我个人只会使用auction和bid
#config/routes.rb
resources :auctions, only: :create do
resources :bids
endAuctions应该是唯一的基于他们的主键无论如何..。
#app/models/auction.rb
class Auction < ActiveRecord::Base
belongs_to :auction
has_many :bids
end
#app/models/bid.rb
class Bid < ActiveRecord::Base
belongs_to :auction
end这将使您能够确定具体的拍卖,然后创建您需要的出价。
https://stackoverflow.com/questions/34368912
复制相似问题