Skip to content

Instantly share code, notes, and snippets.

@jcalvert
Created February 16, 2012 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcalvert/1848467 to your computer and use it in GitHub Desktop.
Save jcalvert/1848467 to your computer and use it in GitHub Desktop.
Make polymorphic routes support scoped parameters

If you are scoped you can do simple form_for now.

If your routes are say like this:

 scope ':organization', :context => do
   resources :employees
 end

You can now do

form_for @employee do |f|
   f.text_field :name 
end

Instead of using ternary operators or other stupid tricks.

module ActionDispatch
module Routing
module PolymorphicRoutes
def polymorphic_path(record_or_hash_or_array, options = {})
begin
polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
rescue Exception => e
polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path).merge(params.reject{|k,v| ["controller", "action"].include? k.to_s}))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment