Brendon Muir
Brendon Muir

Reputation: 4612

Polymorphic urls with singular resources

I'm getting strange output when using the following routing setup:

  resources :warranty_types do
    resources :decisions
  end
  resource :warranty_review, :only => [] do
    resources :decisions
  end

I have many warranty_types but only one warranty_review (thus the singular route declaration). The decisions are polymorphically associated with both. I have just a single decisions controller and a single _form.html.haml partial to render the form for a decision.

This is the view code:

= simple_form_for @decision, :url => [@decision_tree_owner, @decision.becomes(Decision)] do |form|

The warranty_type url looks like this (for a new decision):

/warranty_types/2/decisions

whereas the warranty_review url looks like this:

/admin/warranty_review/decisions.1

I think because the warranty_review id has no where to go, it's just getting appended to the end as an extension.

Can someone explain what's going on here and how I might be able to fix it?

I can work around it by trying to detect for a warranty_review class and substituting @decision_tree_owner with :warranty_review and this generates the correct url, but this is messy. I would have thought that the routing would be smart enough to realise that warranty_review is a singular resource and thus discard the id from the URL.

This is Rails 3 by the way :)

Upvotes: 3

Views: 890

Answers (1)

Brendon Muir
Brendon Muir

Reputation: 4612

Apparently it's a long standing rails bug where polymorphic_url has no way of knowing whether a resource is singular or not from the routes setup:

https://rails.lighthouseapp.com/projects/8994/tickets/4077-wrong-redirect-after-creation-of-nested-singleton-resource-using-responder

I'm just going to resort to using a non-singular route even though there will only ever be one warranty_review. It's just aesthetics at the end of the day.

Upvotes: 2

Related Questions