Jerome
Jerome

Reputation: 6189

Rails path helper generate dot or undefined method instead of slash

Route defined as follows

 resources :purchases do
    collection do
       put :wirecardtest
    end
  end

Controller actions redirect in one of the following manners with associated error generated

format.html { redirect_to wirecardtest_purchase_path(@purchase)

undefined method `wirecardtest_purchase_path'

format.html { redirect_to wirecardtest_purchases_path(@purchase)

/purchases/wirecardtest.44

Behaviour is identical when putting code in view.
The ressource is defined in plural mode, as it ought to be. The redirect, as it is supposed to call a specific ressource should call the singular model-action mode (in plural it would generate the period).
I don't understand how I got into this damned-if-you-do, damned-if-you-don't position.

wirecardtest_purchases PUT    /purchases/wirecardtest(.:format)              purchases#wirecardtest

Upvotes: 0

Views: 451

Answers (1)

Shaunak
Shaunak

Reputation: 18018

That's your mistake right there.. the path is generated as 'wirecardtest_purchases' but you are using 'wirecardtest_purchase' note the missing 's' to pluralize the 'purchase'.

Remember its a collection. So the path method is pluralized by rails.

When in doubt rake routes :)

---Update---

Improving the answer (check comments). Need here is to actually define a route as :member and not a :collection if you want to act upon a single object. Referring to Rails Docs,

resources ::purchases do
  member do
    get 'wirecardtest'
  end
end

Upvotes: 1

Related Questions