module ActionDispatch::Routing::Mapper::CustomUrls
def resolve(*args, &block)
NOTE: The +resolve+ method can't be used inside of a scope block such as
to the URL helper that gets called.
array passed to +polymorphic_url+ is a hash then it's treated as options
This generates the URL "/basket#items" because when the last item in an
end
[:basket, options]
resolve "Basket", anchor: "items" do |basket, options|
needs to be two as the instance is passed as the first argument, e.g:
You can pass options to a polymorphic mapping - the arity for the block
the standard polymorphic URL of "/admin/users/1".
The first +link_to+ will generate "/profile" but the second will generate
link_to "Profile", [:admin, @current_user]
link_to "Profile", @current_user
# app/views/application/_menu.html.erb
resolve("User") { [:profile] }
end
resources :users
namespace :admin do
resource :profile
# config/routes.rb
a single model instance is passed and not more complicated forms, e.g:
NOTE: This custom behavior only applies to simple polymorphic URLs where
+link_to+ or +form_for+ instead of the standard "/baskets/:id".
This will now generate "/basket" when a +Basket+ instance is passed to
end
[:basket]
resolve "Basket" do
resource :basket
+link_to+ and +form_for+ when passed a model instance, e.g:
behavior of +polymorphic_url+ and consequently the behavior of
Define custom polymorphic mappings of models to URLs. This alters the
def resolve(*args, &block) unless @scope.root? raise RuntimeError, "The resolve method can't be used inside a routes scope block" end options = args.extract_options! args = args.flatten(1) args.each do |klass| @set.add_polymorphic_mapping(klass, options, &block) end end