Polymorphic urls with singular resources

Posted by Brendon Muir on Stack Overflow See other posts from Stack Overflow or by Brendon Muir
Published on 2011-03-11T06:50:14Z Indexed on 2011/03/11 8:10 UTC
Read the original article Hit count: 243

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 :)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails-3