form_for and scopes, rails 3

前端 未结 7 2082
执念已碎
执念已碎 2020-12-19 05:37

I have a problem due to scopes and the form_for helper in rails 3. The routes - file looks like this:

scope \"(/:tab)\" do
  resources :article
end
         


        
7条回答
  •  伪装坚强ぢ
    2020-12-19 05:57

    I have found this to be a really irritating problem and have gotten around this for now with the following monkey patch. Generic like this, it's a little bid off as you're simply passing the entire bag of parameters to polymorphic_url which is what form_for uses under the hood to guess the route. A more concise approach would be to merge just the scope value.

    My solution:

    https://gist.github.com/1848467

    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
    

提交回复
热议问题