0

I have a routes file (Rails 4.0.4 App):

resources :products do
  resources :variants, except: :index
end

rake routes gives:

       product_variants POST   /products/:product_id/variants(.:format)             variants#create
    new_product_variant GET    /products/:product_id/variants/new(.:format)         variants#new
   edit_product_variant GET    /products/:product_id/variants/:id/edit(.:format)    variants#edit
        product_variant GET    /products/:product_id/variants/:id(.:format)         variants#show
                        PATCH  /products/:product_id/variants/:id(.:format)         variants#update
                        PUT    /products/:product_id/variants/:id(.:format)         variants#update
                        DELETE /products/:product_id/variants/:id(.:format)         variants#destroy

So as proposed I'm using

<%= form_for [@product, @variant] do |f| %>

but this generates (note the URL):

<form accept-charset="UTF-8" action="/variants/1" class="edit_variant" id="edit_variant_1" method="post">

And I get (of course) the routing error saying no route matches with PATCH /variants/1

Same strange behaviour when I redirect to in an Api Controller living namespaced under api/:

redirect_to [:api, @product, @variant]

Got me: no route matches with /api/variants/1

So:

  • I checked all routes
  • I know how to nest resources and namespace them
  • I thought I know how to generate the URL's ;-)

Somehow the @products is ignored when generating the urls

Has anyone a clue where to look any further?

1
  • 4
    You can confirm that the @product variable is not nil. Commented Apr 15, 2014 at 16:49

2 Answers 2

1

Take a look at your variants controller. Are you instantiating @product?

It may be helpful to post the code for controller too.

Hope this helps!

Alex

Sign up to request clarification or add additional context in comments.

Comments

0

Okay the problem really lies in the different controllers. I somehow instantiated @products in a before action, but obviously did it wrong.

Seems to be a leck of energy drink problem. thanks for pointing me to the right solution!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.