Mezbah
Mezbah

Reputation: 1267

undefined method `product_bids_path'

I following this tutorial, when I want to show my product, i'm getting this error:

undefined method `product_bids_path'

in my routes file:

  devise_for :users
  resources :products do
    resources :auctions, only: [:create] do
      resources :bids, only: [:create]
    end
  end

in products/show.html.erb

=render "auction"
=render "bid"

here is my bid partial:

= form_for [ @product, @product.auction, Bid.new] do |f|

Here in this form_for helper its need to send product_auction_bids_path url request but its sending product_bids_path url request.

How should i write my correct form_for helper that send request to product_auction_bids_path

Upvotes: 1

Views: 67

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

Seems like the comments solved it;

For the benefit of readers, if you have a nested set of routes (as follows), you have to make sure you call the path with all the dependent objects:

resources :products do
    resources :auctions, only: [:create] do
      resources :bids, only: [:create]
    end
end

products_auctions_bids_path(@product, @auction, @bid)

Now.

There's an important point to be made about this -- the docs state that you should not nest routes beyond more than one level:

Resources should never be nested more than 1 level deep.

You really need to make sure you are able to call a route based on less unique information than the primary key's of all three nested resources.

I know you're not calling the individual paths (only create), but it's still a bad pattern to nest it all like you're doing.

There are several ways to remedy it; I would personally just use auction and bid:

#config/routes.rb
resources :auctions, only: :create do
   resources :bids
end

Auctions should be unique based on their primary key anyway...

#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

This will allow you to identify the auction specifically, and then create bids as you require.

Upvotes: 1

Related Questions