Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 2.19 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Rails routing - how to add scope param to url_for helper?
  2. = link_to "Add new bio", [:new, :admin, :bio]
  3.        
  4. namespace :admin do    
  5.   scope "/:bio_type", :defaults => {:bio_type => "company"} do
  6.     resources :bios
  7.   end
  8. end
  9.        
  10. = link_to "Add new bio", [:new, :admin, :bio, { bio_type: params[:bio_type] }]
  11.        
  12. def url_for(options = {})
  13.     options ||= {}
  14.     case options
  15.     when String
  16.       options
  17.     when Hash
  18.       options = options.symbolize_keys.reverse_merge!(:only_path => options[:host].nil?)
  19.       super
  20.     when :back
  21.       controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
  22.     else
  23.       polymorphic_path(options)
  24.     end
  25.   end
  26.  
  27.  
  28.   def polymorphic_path(record_or_hash_or_array, options = {})
  29.     polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
  30.   end
  31.  
  32.   def polymorphic_url(record_or_hash_or_array, options = {})
  33.     if record_or_hash_or_array.kind_of?(Array)
  34.       record_or_hash_or_array = record_or_hash_or_array.compact
  35.       if record_or_hash_or_array.first.is_a?(ActionDispatch::Routing::RoutesProxy)
  36.         proxy = record_or_hash_or_array.shift
  37.       end
  38.       record_or_hash_or_array = record_or_hash_or_array[0] if record_or_hash_or_array.size == 1
  39.     end
  40.  
  41.     record = extract_record(record_or_hash_or_array)
  42.     record = convert_to_model(record)
  43.  
  44.     args = Array === record_or_hash_or_array ?
  45.       record_or_hash_or_array.dup :
  46.       [ record_or_hash_or_array ]
  47.  
  48.     inflection = if options[:action] && options[:action].to_s == "new"
  49.       args.pop
  50.       :singular
  51.     elsif (record.respond_to?(:persisted?) && !record.persisted?)
  52.       args.pop
  53.       :plural
  54.     elsif record.is_a?(Class)
  55.       args.pop
  56.       :plural
  57.     else
  58.       :singular
  59.     end
  60.  
  61.     args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)}
  62.     named_route = build_named_route_call(record_or_hash_or_array, inflection, options)
  63.  
  64.     url_options = options.except(:action, :routing_type)
  65.     unless url_options.empty?
  66.       args.last.kind_of?(Hash) ? args.last.merge!(url_options) : args << url_options
  67.     end
  68.  
  69.     args.collect! { |a| convert_to_model(a) }
  70.  
  71.     (proxy || self).send(named_route, *args)
  72.   end
  73.        
  74. polymorphic_path([:new, :admin, :bio], bio_type: params[:bio_type])